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

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

Related

Is there a way to test the output of a jupyter Notebook file in a python file?

I am currently writing a automated testing program for files in jupyter Notebook.
I want to do this without executing the jupyter Notebook, so i want to test in a .py file.
Is there a way to do that?
Another aspect would be to turn the notebook to a .py file and then test is, but how can i access certain cells within it?

How to access the JSON of a (non-saved) Jupyter notebook from within it?

How can the (JSON) contents of a Jupyter notebook (i.e. what would be saved in a .ipynb file) be accessed programmatically from within the notebook?
There is the option of forcing the notebook to be saved and then reading back the .ipynb file, but I prefer to not clobber the current .ipynb. Maybe there is a way of at least saving it under another path? That would not be ideal, but that would work.
According to #krassowski in the comments to the question, accessing the JSON of a Jupyter Lab notebook from within itself is not currently doable.

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.

Convert Markdown to pdf with Jupyter Notebook

I'm working in a jupyter notebook, and I'm working on a markdown file. I'm using a mac with Sierra 10.12.16. I'd like to convert the markdown to a pdf and I'm wondering what the easiest way to do that would be. I've read a lot of posts about installing things like pandoc or using packages in atom. I'm wondering if there isn't a simple way with the jupyter notebook to convert the markdown file to pdf. It really seems like there should be, but I'm having trouble finding it.
I figured it out. You can just paste the markdown code in to a cell in a jupyter notebook and change the dropdown menu below "Widgets" in the notebook from "code" to "markdown". Then hit shift enter and it renders the markdown. You can then download the notebook as pdf.

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