I am using an online jupyter notebook that is somehow configured to read all .py files as jupyter notebook files:
I am a big fan of this setup and would like to use it everywhere. On my own jupyter installation however, .py files are just interpreted as test files and are not by default loaded into jupyter cells. How can I achieve the same configuration for my jupyter notebook?
What you're looking for is jupytext.
You just need to install it into python env from which you're running your jupyter notebooks:
pip install jupytext --upgrade
And you get this:
That's not exactly what you asked, but you can achieve something close to that by using the magic %load FILE.py in a new jupyter notebook.
%load FILE.py will copy the contents of FILE.py in the current cell upon executing it.
You use the python code in your Jupyter Notebook by just pasting the whole code in a cell OR :
%load pythonfile.py to load code from a file (not necessarily .py files) into a jupyter notebook cell;
%run pythonfile.py in order to execute the file instead of loading it (outputs whatever that file outputs).
Also, pythonfile.py should exist in the cd or you can use its full path.
Related
Context: I'm using vscode with WSL and I also use conda for environment management.
I'm trying to create a variable in a jupyter notebook, let's say x = [10, 20], and then use that same variable in a .py file (not on jupyter notebooks). I'm already using the same environment on both, but the terminal/kernel I believe is different for each. I believe this because when I run a cell on jupyter notebook, nothing happens on the terminal. However, when I run on .py files, the terminal runs the code I selected.
I would like to see the terminal running something for jupyter (.ipynb) and also for my .py files.
Any help would be really appreciated.
I have created a my own library(package) and installed as development using pip install -e
Now, I would like to edit this library(.py) files and see the update in jupyter notebook. Every time, I edit a library(.py) files I am closing and reopening ipython notebook to see the update. Is there any easy way to edit and debug .py package files ?
Put this as first cell of your notebooks:
%load_ext autoreload
%autoreload 2
More info in the doc.
When you load jupyter, you are initializing a python kernel. This will lock your python to the environment it was at when you loaded the kernel.
In this case, your kernel contains your local egg installed package at the point where it was when you loaded jupyter. Unfortunately, you will need to reload jupyter every time you update your local package.
#BlackBear has a great solution of using autoreload in your first cell:
%load_ext autoreload
%autoreload 2
A follow up solution assumes you do not need to make changes to your notebooks, but just want the updated outputs given changes to your package. One way I have gotten around this is to use automated notebook generation processes using jupyter nbconvert and shell scripting. You essentially create some jupyter templates stored in a templates folder that you will auto execute every time you update your package.
An example script:
rm ./templates/*.nbconvert.ipynb
rm ./*.nbconvert.ipynb
for file in "templates"/*.ipynb
do
echo $file
jupyter nbconvert --to notebook --execute $file
done
mv ./templates/*.nbconvert.ipynb .
Assuming you want to actively debug your package, I would recommend writing test scripts that load a fresh kernel every time. EG:
#mytest.py
from mypackage import myfunction
expected_outputs={'some':'expected','outputs':'here'}
if myfunction(inputs)==expected_outputs:
print('Success')
else:
print('Fail')
python3 mytest.py
I have a project containing a bunch of Python modules (.py files) and a bunch of Jupyter Notebooks (.ipynb files) which import things from the Python modules.
I can (assuming I've got __init__.py files in all subfolders) type-check all the .py files by simply running mypy . from the root of the project. But I'd like to also be able to type-check my Jupyter Notebooks.
An ideal solution would:
type check all Python code in my Jupyter Notebooks,
be able to follow imports of .py modules from within Jupyter Notebooks when type-checking, just like imports in .py files,
let me type-check the whole project from the command line, so that I can run type-checking as part of a test suite or a pre-commit hook, and
in some way meaningfully report the locations of type errors within my Notebooks, analogously to how mypy prints line numbers for errors in .py files.
How can I do this?
You could use nbQA and do
pip install -U nbqa
nbqa mypy your_notebook.ipynb
You can:
Convert all notebooks to python, then run mypy on that (How do I convert a IPython Notebook into a Python file via commandline?).
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
Just write a small script to do this and you are fine :)
Checkout nb-mypy
Nb Mypy is a facility to automatically run mypy on Jupyter notebook cells as they are executed, whilst retaining information about the execution history.
More details here
I use Jupytext and my IDE for this.
I export a copy in py:percent format, link that to the notebook. I Do the development in the jupyter lab environment, but the .py file is the one that goes in the git repository. Before commiting, I run it throught the usual linters, black, pydocstyle, mypy (with a strict configuration). I then reload the notebook in Jupyter lab, restart the kernel and 'Run All' to make sure the results are still OK, and only then commit the file to the repository
I have a machine learning project in drive D. It contains python notebook files. But when I launch jupyter notebook through command prompt, it only shows directories inside drive C. How can I navigate to a drive other than C drive without changing jupyter notebook startup path?. I'm using windows 10 operating system. All the solutions I found, trying to change notebook startup path.
You can launch it from any drive/location you want with the --notebook-dir option.
For example:
jupyter notebook --notebook-dir=D:/
Follow these steps:
Open Anaconda Prompt
Insert this command,
jupyter notebook --notebook-dir=NameOfTheDrive:.
As Example for Drive D, jupyter notebook --notebook-dir=D:
To permanently change the default directory of Jupyter do the following:
Look for the Shortcut to open Jupyter notebook.
Right Click on the Shortcut
Go to Properties.
Look for ....jupyter-notebook-script.py "%USERPROFILE%/"
Change %USERPROFILE%/ to the folder you wish.
Restart Jupyter notebook.
Open the Anaconda Prompt
type the following:
jupyter notebook --notebook-dir=D:\
This is what worked for me in windows.
Okay, this question seems to have already been addresses, but I'd like to add quick step launch solution for this case. I created a batch file (windows .bat file) with the following script and saved it on my desktop. I just double click it to launch the files in the respective drive.
#echo ON
title Launch Jupyter notebooks from Drive D
jupyter notebook --notebook-dir=D:
#echo OFF
Copy paste this code in a text file and save it as a *.bat file on your Desktop. Just fire it up every time you want to launch Jupyter.
You can create various versions of this file for each drive as required and keep them handy.
Another good option is from your terminal go to the disk you want to start jupyter by example :
cd D:
And then launch jupyter notebook
jupyter notebook
If you are using JupyterLab, just type in your desired terminal;
jupyter lab --notebook-dir=D:/
It will create a new port to access D: drive in JupyterLab
Open Jupyter notebook as usual
Click on New\Terminal
Type the code below in terminal, where you want to point to F drive
jupyter notebook --notebook-dir=F:/
Jupter notebook new session url will open in your default internet explorer
you can directly access by following command:
jupyter notebook --notebook-dir=D:/
Alternate method is go to Anaconda Prompt or Anaconda Termial
then Access D drive and launch the jupyter notebook
D:
jupyter notebook
*To change just one time :
user following code in your Anaconda prompt .
jupyter notebook --notebook-dir=E:
*To permanently change the default directory of Jupyter do the following:
Right click on Jupyter notebook icon.
Go to Properties.
In shortcut tab->target you will find something like this -> jupyter-notebook-script.py "%USERPROFILE%/"
Change %USERPROFILE%/ to the folder you want to start from eg : jupyter-notebook-script.py "E:\folder_path".
Restart Jupyter notebook.
My solution is to create a batch file which allows me to execute jupyter at the current directory.
Create a batch file jupy.bat with the following content:
jupyter notebook --notebook-dir=%1
Save the jupy.bat in some folder, let say in d:\scripts
Append d:\scripts in PATH environment variable. So that i can call jupy just everywhere.
From now on, i can go to the folder where i store my notebooks, and execute the command:
jupy .
This is to open Jupiter lab from E Drive
Open Anaconda Prompt
Type the code below in terminal, where you want to point to E drive
jupyter lab --notebook-dir=E:/
My repositories are in the drive R.
I am running Jupyter on Windows 10 in the Ubuntu 18.04, so the drive R is at /mnt/r.
$ nano ~/.bashrc
alias notebook="jupyter notebook --notebook-dir=/mnt/r"
$ source ~/.bashrc
$ notebook
It does not open the browser automatically, so I have to copy the URL manually.
It may ask you to enter the token which you can get by executing:
$ jupyter notebook list
The most easiest solution in my opinion
Open Notepad
Paste the command "jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10"
Save the notepad file with an extension of ".bat" instead of ".txt"
Paste the file in which directory you want to initialize your jupyter
Double click and open the ".bat" file
Jupyter opens with desired directory as base
This way you control the jupyter root directory as and when required and don't
really have to perform any manual settings
Hope this helps
".bat" file created on Desktop
".bat" file double clicked and executed
jupyter opens with Desktop as the intended base directory
I have a jupyter notebook with both html and python code in it. Is it possible to write a script that will launch the notebook and run it in the browser? Most solutions on the web refer to running these scripts from the command line, but I want them to show up on the
I'm not sure what OS you're on, but here's a small batch file that moves into my directory with my .ipynb files, starts Jupyter, and then opens a specific notebook of mine:
cd "%userprofile%\desktop\att"
start chrome.exe http://localhost:8888/notebooks/ATT_SQL.ipynb#
jupyter notebook
I just put this on my desktop and double click it to start Jupyter. Just replace your notebook's name where mine is ATT_SQL.ipynb#. You'll also have to change the cd command as well.
Edit:
Or better yet:
cd %userprofile%\path\to\your\jupyter\dir
jupyter notebook yourNotebook.ipynb
Source