Importing once exported .py file back to IPython Notebook - python

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.

Related

Is it possible to add table of contents like to python script?

I love Jupyter notebook. One of reasons is it has table of contents which is very convenient to search and organize big codes. However Jupyter notebook or Jupyter lab is very slow for a big notebook file. So I am thinking to convert notebook back to py file.
I am wondering if there is any IDE where I can add markdown to the script and then can add table of contents to py file like notebook does?
Thanks

Convert .py to .ipynb file in pycharm

As Jupyter Notebook integration is available in PyCharm professional, is there a GUI option to convert a .py to a .ipynb file?
I tried to edit the file extension, e.g. from test.py to test.ipynb, but then I can't open the .ipynb file.
Simplest way I found without installing extra dependencies is:
Create a new notebook: File > New > Jupyter notebook
Copy and paste the Python code into it
Then you can add #%% markers to separate the cells.
If all you want is just to run your code cell by cell directly on Pycharm's console, you don't need to convert it from .py to .ipynb.
Just activate View > Scientific Mode and add #%% markers to separate the cells directly in your .py file. Now you can execute it by using the green arrows on the left side of your code.
Tested on Pycharm professional 2020.3.2
Try using
pip install ipynb-py-convert
https://medium.com/#researchplex/the-easiest-way-to-convert-jupyter-ipynb-to-python-py-912e39f16917

Run from and save to .py file from Jupyter Notebook

On my old computer, I was able to run .py files from Jupyter Notebook, edit them, and run them. The .py file was effectively a notebook file for all intents and purposes. I updated to the latest version of notebook, and I am no longer able to do this. How do I use .py files on my notebook?
I know there are roundabout ways to do this. I am looking for the method where, when you are in notebook, instead of opening a .ipynb file, you select a .py file which is opened, and behaves like a .ipnyb. When you save it, it writes to .py.
A text file can be loaded in a notebook cell with the magic command %load.
If you execute a cell containing:
%loadpy filename.py
The content of filename.py will be loaded in the next cell. You can edit and execute it as usual.
To save the cell content back into a file add the cell-magic
%%writefile filename.py at the beginning of the cell and run it.
To see the help for any magic command add a ?: like %loadpy? or %%writefile?.
%COMMAND-NAME?
i.e. %run?
For list of available magic function use %lsmagic.
Alternatively there is also another method magic function called %save-f but I would not recommend that, it's an indirect way of saving files.
Also see -
1. Magic Functions docs
2.this nbviewer for further explanation with examples. Hope this helps.
This is not the exact answer. At one point, I was able to open .py files using python notebook and work on it as if it were a notebook file.
However, I have been able to replicate this behavior using VScode.
https://code.visualstudio.com/docs/python/jupyter-support-py
Using VScode, you can export all your .ipynb files into .py files, then run code blocks. Code blocks are separated by # %%.
I have not used it sufficiently long enough to decide if it is better than python notebook, but this seems to be the best solution so far. I previously tried using Atom/Hydrogen and did not enjoy the experience.
You can save individual cells as files using the following code: %%writefile some_file_name.py.
You can run that code straight from the terming or from another notebook using the following code: %run some_file_name.py
Some editors (like spyder and vscode) have jupyter notebook functionality. These can be used if jupyter in installed in the python environment.
You can use it by add #%% on top of the block of code. (in vscode the button 'run cell' will automatically appear)
Also it is possible to import .ipynb as .py which can be run in to fancy decrypt above.
I just found this package p2j and tested it with a .py file with functions, comments and normal code.
I used it as indicated in this answer by doing the following:
pip install p2j
p2j -o script.py -t new_file.ipynb
You can also add -o flag to overwrite the original file.
With this, I got a working Jupiter Notebook with each block of code in a cell and the comments as markdown.
Example:
Original .py script
Converted .ipynb

What is the difference between a .py file and .ipynb file?

I have read about .py and .ipy, also the difference between python, ipython and notebook.
But the question is: what is the real difference between .py and .ipynb file?
Is .ipynb file just more convenient to be run on jupyter notebook, or anything more? I am wondering because I am thinking about which format to be used for publishing on GitHub.
Thanks
.py is a regular python file. It's plain text and contains just your code.
.ipynb is a python notebook and it contains the notebook code, the execution results and other internal settings in a specific format. You can just run .ipynb on the jupyter environment.
Better way to understand the difference: open each file using a regular text editor like notepad (on Windows) or gedit (on Linux).
Save on git the .ipynb if you want to show the results of your script for didatic purposes, for example. But if you are going to run your code on a server, just save the .py
Adding #Josir answer, the below information is very useful for open .ipynb file using PyCharm.
Create a new Python project in Pycharm
Specify a virtual environment, and install the jupyter package(pip install jupyterlab).
Run the server using the jupyter-lab command.
Browser will open the jupyter notebook like below, there you can execute the .ipynp file.
Here is documentation https://jupyterlab.readthedocs.io/en/latest/
py means PYthon
ipynb means Interactive PYthon NoteBook - which is now known as Jupyter notebook.
The latter one is merely a Python script with descriptive contents - you describe what your data is doing by means of Python script and some funny texts. That's pretty much it - and also, you need a specific editor e.g. PyCharm or Google Collab to open and run it.
I think the answer here might help you: https://stackoverflow.com/a/32029027/11924650
.ipy indicates that it's an IPython script. The only difference between IPython scripts and normal Python scripts is that IPython scripts can use IPython magics, e.g. %timeit, and run system commands as !echo Hi.

Downloaded .py file not working in Jupyter

I'm new to programming and now taking the course MIT 6.0001. On problem set 2, they set a .py file, accompanied with a .txt file.
I ran .py file in Spyder with no difficulties (.txt file embedded just fine).
But when I run .py file in Jupyter, things won't run (as in screenshot).
I want to use Jupyter because it's much clearer than Spyder, and can give headers to cells to keep myself on track. Please help me to run things in Jupyter.
Here are link to the problem set (ZIP file)
File not running in Jupyter
Use
%load filename.py
to import the text of your python file into a cell in a Jupyter notebook and then save that notebook.
Jupyter isn't built to run/edit .py files, it's built to run/edit jupyter notebooks (ipython notebooks) (.ipynb)

Categories