Run from and save to .py file from Jupyter Notebook - python

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

Related

How can i stop vs code from making .ipynb files?

When i run example.ipynb file in vs code, it saves me a lot of files like:
example-caloifdf-498s-a54d-sdj8-caksjnbrhn83.ipynb
example-a60c6s44-9fax-4747-a9da-as9876fg34r8.ipynb
etc...
in the same folder where example.ipynb is in.
Questions:
What are those files?
How i can i stop vs code from making them\put them in another path if they are relevant?
Thank You.
That should actually be a bug. It's supposed to delete those files. There a consequence of us using jupyter under the covers to run your kernel.
This bug here should resolve it when we fix it:
https://github.com/microsoft/vscode-jupyter/issues/6510
.ipynb files aren't created by vs-code, they are jupyter notebook files generated when working with jupyter notebooks. They don't appear in anaconda or other IDEs which have built-in jupyter notebook support because there is no need to interact with the underlying .ipynb file directly.

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

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.

Get jupyter notebook to print a whole function that it has imported

I had a python function saved in a .py file but I just accidentally wrote over it.
However, I have a jupyter notebook open and loaded with the function before I erased it (so I guess it is using the .pyc ?). So at the beginning of my notebook:
from now_deleted_script import deleted_function
Is it possible for me to tell the jupyter notebook to print out the whole deleted_function that it has stored in its memory?
You may be able to use %ed deleted_function to see the original source code.

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

Categories