load notebook as module in ipython - python

I want to load the content of a notebook as a module in ipython like we do in normal cases. We add a file with the name __init__.py in the directory of the module of interest and that enable us to load it from another python file by doing from fileName import *. Now I can't get it work with ipython. Can anyone help me to figure this out ?
Many thanks.

The notebook format is not Python code. You can convert it using nbconvert, e.g.:
ipython nbconvert --to python notebook.ipynb

Related

Jupyter Class in different notebook?

i am currently forced in an project to work with jupyter notebook, before I just used PyCharm.
So I create a project and would like to add some classes to it. Should I write all of them in the one main notebook or create different notebooks for each class?
As far as I understand, jupyter is not made for using several notebooks for one project (but I thnink there are options) but if I want to use some of my classes more general for other projects it does not make sense to not have it in a seperate notebook right?
I am sorry, maybe I have a basic misunderstanding of jupyter but I could not figure out yet, what is the best way.
Jupyter will happily allow you to use multiple libraries in other notebooks. You can find the documents here
Though if you want to move the project out to a command line executable, you may want to consider exporting the notebooks as .py files and then importing them as standard python libraries. Jupyter has a nbconvert method for this.
I do this a lot and ended up writing a small script that adds appropriate hashbang lines and strips out any Jupyter "magic" commands. You can find it at github.
Usage: ./nbconvert myNotebook.ipynb
There are also some excellent Jupyter magic commands that make working with libraries much easier.
# cause chagnged modules to be reloaded at execution
# add this to any notebook that sources an external .py file
%load_ext autoreload
%autoreload 2
I also find this useful:
# set %magic alias to convert the current notebook into a .py file
%alias nbconvert ~/devtools/nbconvert thisNotebook.ipynb
Usage:
%nbconvert
[NbConvertApp] Converting notebook searchTools.ipynb to python
You don't necessarily need notebooks, you may create .py files with your class implementations for example myclass.py, and then just normally do import myclass

Python Modules & Packages using jupyter notebook in Google Cloud Platform

I'm using Jupyter Notebooks within Google CloudDatalab in Google Cloud Platform for my Python scripts. This creates .ipynb files.
I want to be able to create Modules and Packages using Jupyter Notebooks but it is not able to import the scripts.
For e.g.:
mymodule.ipynb has this:
def test_function():
print("Hello World")
Then in myscript.ipynb when I try to import the above:
from mymodule import test_function
It throws an error :
*ImportErrorTraceback (most recent call last) in ()
----> 1 from mymodule import test_function ImportError: No module named mymodule*
How do I create Modules & Packages using Jupyter Notebooks?
Notebooks can't be used as modules. You need to create a python file (e.g. mymodule.py).
If you want you can do this from within a jupyter notebook:
with open('mymodule.py', 'w') as f:
f.write('def test_function():')
f.write(' print("Hello World")')
from mymodule import test_function
test_function()
# Hello World
You cannot import Jupyter Notebook in the same way as Python files (packages).
Check this link: https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html
I've received a response on a different forum which answered my question. Here is the answer:
Put your module code into a .py file and make sure that's in the same directory as myscript.ipynb.
To create the .py file from the code currently in a notebook, you can download it from within Jupyter as a .py file and tidy it up in your text editor of choice.
Remember, the import statement is pure python syntax. It knows nothing about - and has no wish to know anything about - jupyter notebooks. It's looking for a .py file.
This has resolved the issue.
Thanks a lot.

How do I view the Python Codes inside a Python Script?

I have a Python Script (with .py extension) and it was created by makepy.py.
Is there a way to view and copy the codes inside that script and load them into my Jupyter Notebook?
I have done a Google search but strangely, I can't find this mentioned anywhere.
Do I need a specific software to do this? Or can it be done at the Python command prompt level?
You can just edit the file with a text editor. Right click and select open with. Then you can copy the code.
All that can be done directly from the Jupyter Notebook or any IPython console.
to view the contents of the script
!cat myscript.py
to run the script as a program use this built-in magic command
%run myscript.py
You can load file in Jupyter notebook cell by:
%load your_file.py
You can do changes to it and than save it back by
%%writefile your_file.py
or
%%file your_file.py

Convert JSON IPython notebook (.ipynb) to .py file

How do you convert an IPython notebook file (json with .ipynb extension) into a regular .py module?
From the notebook menu you can save the file directly as a python script. Go to the 'File' option of the menu, then select 'Download as' and there you would see a 'Python (.py)' option.
Another option would be to use nbconvert from the command line:
jupyter nbconvert --to script 'my-notebook.ipynb'
Have a look here.
According to https://ipython.org/ipython-doc/3/notebook/nbconvert.html you are looking for the nbconvert command with the --to script option.
ipython nbconvert notebook.ipynb --to script
In short: This command-line option converts mynotebook.ipynb to python code:
jupyter nbconvert mynotebook.ipynb --to python
note: this is different from above answer. ipython has been renamed to jupyter. the old executable name (ipython) is deprecated.
More details:
jupyter command-line has an nbconvert argument which helps convert notebook files (*.ipynb) to various other formats.
You could even convert it to any one of these formats using the same command but different --to option:
asciidoc
custom
html
latex. (Awesome if you want to paste code in conference/journal papers).
markdown
notebook
pdf
python
rst
script
slides. (Whooh! Convert to slides for easy presentation 😊)
the same command jupyter nbconvert --to latex mynotebook.ipynb
For more see jupyter nbconvert --help. There are extensive options to this. You could even to execute the code first before converting, different log-level options etc.
you can use this to do that :
pip install ipynb-py-convert
then run this on your terminal to convert .py file to .ipynb :
ipynb-py-convert Ex/abc.py Ex/abc.ipynb
to convert .ipynb files to .py :
ipynb-py-convert Ex/abc.ipynb Ex/abc.py
Tested on Ubuntu 20.04
Install required packages for PIP
$ pip install ipython
$ pip install nbconvert
To install required packages
$ sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic
METHOD 1
Use jupyter nbconvert command to convert to different format
Source file pro.ipynb
To convert to ascii
$ jupyter nbconvert --to asciidoc pro.ipynb
To convert to pdf
$ jupyter nbconvert --to pdf pro.ipynb
To convert to python
$ jupyter nbconvert --to python pro.ipynb
METHOD 2
Convert ipynb project through python code use savefig method of pyplot:
pro.ipynb
import matplotlib.pyplot as plt
%matplotlib inline
exp_vals=[1400,600,300,410,250]
exp_labels=['Home Rent','Food','Phone/Internet Bill','Car','Other Utilities']
plt.axis('equal')
plt.pie(exp_vals,labels=exp_labels,radius=2,autopct='%0.1f%%',shadow=True,explode=[0,0.5,0,0.3,0],startangle=20)
# plt.show()
plt.savefig('piechart.jpg',bbox_inches='tight',pad_inches=2,transparent=True,edgecolor='r')
piechart.png image that it generated:
Hope this helps to convert your ~(`)/\/\/\_ [Python] code
well first of all you need to install this package below:
sudo apt install ipython
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
two options are available either --to python or --to=python
for me this works fine:
jupyter nbconvert --to python while.ipynb
[NbConvertApp] Converting notebook while.ipynb to python
[NbConvertApp] Writing 758 bytes to while.py
pip3 install ipython
if it does not work for you try, by pip3.
pip3 install ipython
You definitely can achieve that with nbconvert using the following command:
jupyter nbconvert --to python while.ipynb
However, having used it personally I would advise against it for several reasons:
It's one thing to be able to convert to simple Python code and another to have all the right abstractions, classes access and methods set up. If the whole point of you converting your notebook code to Python is getting to a state where your code and notebooks are maintainable for the long run, then nbconvert alone will not suffice. The only way to do that is by manually going through the codebase.
Notebooks inherently promote writing code which is not maintainable (https://docs.google.com/presentation/d/1n2RlMdmv1p25Xy5thJUhkKGvjtV-dkAIsUXP-AL4ffI/edit#slide=id.g3d7fe085e7_0_21). Using nbconvert on top might just prove to be a bandaid. Specific examples of where it promotes not-so-maintainable code are imports might be sprayed throughout, hard coded paths are not in one simple place to view, class abstractions might not be present, etc.
nbconvert still mixes execution code and library code.
Comments are still not present (probably were not in the notebook).
There is still a lack of unit tests etc.
So to summarize, there is not good way to out of the box convert python notebooks to maintainable, robust python modularized code, the only way is to manually do surgery.
You can run a .py file in the same directory:
import json
files = ["my_first_file_name.ipynb", "my_second_file_name.ipynb"]
for file in files:
code = json.load(open(file))
py_file = open(f"{file}.py", "w+")
for cell in code['cells']:
if cell['cell_type'] == 'code':
for line in cell['source']:
py_file.write(line)
py_file.write("\n")
elif cell['cell_type'] == 'markdown':
py_file.write("\n")
for line in cell['source']:
if line and line[0] == "#":
py_file.write(line)
py_file.write("\n")
py_file.close()
I rewrite this code from Syrtis Major's answer.
You can use the following script to convert jupyter notebook to Python script, or view the code directly.
To do this, write the following contents into a file cat_ipynb, then chmod +x cat_ipynb.
#!/usr/bin/env python
import sys
import json
for file in sys.argv[1:]:
print('# file: %s' % file)
print('# vi: filetype=python')
print('')
code = json.load(open(file))
for cell in code['cells']:
if cell['cell_type'] == 'code':
print('# -------- code --------')
for line in cell['source']:
print(line, end='')
print('\n')
elif cell['cell_type'] == 'markdown':
print('# -------- markdown --------')
for line in cell['source']:
print("#", line, end='')
print('\n')
Then you can use
cat_ipynb your_notebook.ipynb > output.py
Or show it with vi directly
cat_ipynb your_notebook.ipynb | view -
Go to https://jupyter.org/
click on nbviewer
Enter the location of your file and render it.
Click on view as code (shown as < />)
One way to do that would be to upload your script on Colab and download it in .py format from File -> Download .py
Convert the Ipynb dir files to .py
import os
for fname in os.listdir():
if fname.endswith('ipynb'):
os.system(f'jupyter nbconvert {fname} --to python')
Jupytext allows for such a conversion on the command line, and importantly you can go back again from the script to a notebook (even an executed notebook). See here.
Copy all the (''.ipynb) files in the Desired folder then execute:
import os
desired_path = 'C:\\Users\\Docs\\Ipynb Covertor'
os.chdir(desired_path)
list_of_directory = os.listdir(desired_path)
for file in list_of_directory:
os.system('ipython nbconvert --to script ' + str(file))
If this is a one-off, follow e.g. #kikocorreoso depending if you want to use command line or gui.
However, if you want some solution that will maintain a synchronized version of the .py and the .ipynb you really should consider using jupytext as also pointed out by #Wayne
Run conda install jupytext or pip install jupytext
Then do:
jupytext --set-formats ipynb,py <file>.ipynb
To keep it synchronized to the .py file:
jupytext --set-formats ipynb,py <file>.ipynb --sync
This will make sure jupyter keeps the two files in sync when saving from now on...
Last note: If you are a gui person, after running the installation command for jupytext, everything else can be done from the gui as well File-->Jupytext-->pair Notebook with light Script:
jupyter nbconvert [filename].ipynb --no-prompt --to python
The above code will generate [filename].py in the same directory
My version of Jupyter Notebook, 3.3.2, has this option on the File Menu:

Importing once exported .py file back to IPython Notebook

I use Ipython Notebook for all python scripting and also making notes with markdown cells.
Sometime back I had downloaded my Ipython Notebook as xyz.py file using File>Download Notebook as .py file option.
Now I need to import the same xyz.py file back into Ipython Notebook.
How can I achieve this?
Have tried creating a new Notebook and in a cell ran
%load xyz.py
but it loads the raw contents of the file in a single cell.
And does not render markdown cells and code cells content as it was there in the original Notebook.
As far as I understand the Python .py file export is one direction process as it loses information in the process.
If you wish to repopen the notebooks for edit use the native JSON (ipynb) format.
For now your option is to reconstruct the cells from the source code by hand.

Categories