from ipynb to py with jupiter lab export - jupyter-notebook

I open Jupyter lab,
I go to
file
Export Notebook As
Export Notebook As Executable Script.
I than get a warning from my system saying:
This type of file can harm your computer. Do you want to keep filename.py anyway?
Is there risk involved? Why do I get the warning? Would you recommend that way to convert my .ipynb to .py?

You are warned because your browser noticed that you are trying to download some .py script. When you download some from the Internet it really can be harmful. But the browser doesn't know that actually, it's your own code. So download it, you'll be fine.

Related

How do I access a .ipynb file? Do I neeed to download jupyter notebook in order to do this?

I'm doing the SQL course on coursera
Yes, you can install Jupyter Notebook as a library for python.
But also you can try open and run your file, for example, in Google Collab.

No code displayed when I open the compiled R notebook

I am using R Studio V 1.1.423. I create and save R Notebook files but once I open the .rmd files in R Studio I only get a completely blank page with no code displayed.
However the html files work fine when opened in the browser.
Any suggestions would be really appreciated.
This seems to be a bug in RStudio: It happens every once in a while. It usually helps to delete the R Notebook output (the .nb.html file) and to recompile it from scratch.
You might also have to clear the cache, which can be found (on Unix-like systems) in ~/.rstudio-desktop/notebooks:
Close RStudio.
Find the subfolder corresponding to your notebook and delete it. The subfolder contains a quasi-random hexadecimal prefix, followed by your notebook name; for instance: A88D397F-notebook.
Reopen RStudio and recompile your notebook from scratch.

jupyter notebook takes forever to open and then pages unresponsive - [MathJax] issue

I'm trying to open a jupyter notebook and it takes a long time and I see at the bottom it's trying to load various [MathJax] extension, e.g. at the bottom left of the chrome browser it says:
Loading [MathJax]/extensions/safe.js
Eventually, the notebook loads, but it's frozen and then at the bottom left it keeps showing that it's trying to load other [MathJax] .js files.
Meanwhile, the "pages unresponsive do you want to kill them" pop up keeps popping up.
I have no equations or plots in my notebook so I can't understand what is going on. My notebook never did this before.
I googled this and some people said to delete the ipython checkpoints. Where would those be? I'm on Mac OS and using Anaconda.
conda install -c conda-forge nbstripout
nbstripout filename.ipynb. Make sure that there is no whitespace in the filename.
I had a feeling that the program in my Jupyter notebook was stuck trying to produce some output, so I restarted the kernel and cleared output and that seemed to do the trick!
If Jupyter crashes while opening the ipynb file, try "using nbstripout to clear output directly from the .ipynb file via command line"(bndwang). Install with pip install nbstripout
I was having the same problem with jupyter notebook. My recommendations to you are as follows:
First, check the size of the .ipynb file you are trying to open. Probably the file size is in MB and is large. One of the reasons for this might be the output of a dataset that you previously displayed all rows.
For example;
In order to check the dataset, sometimes I use pd.set_option('display.max_rows', None) instead of the .head() function. And so I view all the rows in the data set.
The large number of outputs increases the file size, making the notebook slower. Try to delete such outputs.
I think this will solve your problem.
Here restarting your kernel will not help. Instead use nbstripout to strip the output from command line.
Run this command -> nbstripout FILE.ipynb
Install nbstripout if it is not there
https://pypi.org/project/nbstripout/
It happened to me the time I decided to print a matrix for 100000 times. The notebook file became 150MB and Jupyter (in Chrome) was not able to open it: it said all the things you experienced and then the page died saying it was "OutOfMemory".
I solved the issue opening it in Visual Studio Code, there is a button "Clear All Output", then I saved the notebook again and it was back to some hundreds of KB, which I could open normally.
If you don't have Visual Studio Code installed, you can open the notebook with another editor (gedit if you use Linux or Notepad++ in Windows) and try to delete the output cells. This is more tricky since you have to pay a lot of attention in what you are deleting, otherwise the notebook will stop working.

Repair corrupted Jupyter notebook / load previous version?

I had a hardware crash while running a Jupyter notebook. After repairing the system and trying to restart the notebook, I got the following error message:
Error loading notebook
Unreadable Notebook: D:\Eddy\Documents\1604 Udacity\1612 Self-driving car Nanodegree\P4\P4 Eduard van Kleef.ipynb NotJSONError("Notebook does not appear to be JSON: ''...",)
Does anyone know of a way to revert to any of Jupyter's previous 'checkpoints'? Or of a way to at least partially restore a JSON?
If you are lucky then the ipynb file is corrupted but still there. In that case you can try opening it in a text file and copying the contents to a new notebook. But check the size of the file. If it is zero bytes, then there is nothing there!
This actually happened to me when my server ran out of memory and somehow the notebook got completely erased. Totally sucks.
Try this
jupyter nbconvert filename.ipynb --clear-output
It worked for me since it has corrupted because of Plotly behavior with some big data.
in your file directory that contains your ipynb file there is a folder called '.ipynb_checkpoints'. this folder does not show in the jupyter application so find it through windows explorer.
inside there's will be a file called urfilenamehere-checkpoint.ipynb
copy paste it to your file directory and open through the jupyter application it should probably work.
if your corrupted file is 0B, you definitely have to rely on the checkpoints.
do not create a new notebook with the same name it will overwrite the checkpoint.

Cannot import .py file to ipython notebook

With apologies in advance for the "I can't get it to work" question: How should I load a .py file into ipython notebook? I want to convert python code to notebooks (first simple scripts and later scripts that include nbconvert directives embedded as comments-- see bottom of the linked file.)
Perhaps I'm doing it wrong, but perhaps there's something wrong with my set-up. When I drag a .py file to the Notebook's file list, I get the message
Invalid file type: Uploaded notebooks must be .ipynb files.
I even tried changing the extension to .ipynb (keeping the python script unmodified); reasonably enough, I got an error:
Error loading notebook: Bad request
Any idea what's going wrong?
System information: I'm on OS X (10.8, Mountain Lion), using Firefox 28.0 and Anaconda 1.9.2 (x86_64), which supplies python 2.7.6 and ipython 2.0. Anaconda is not on the default PATH; I add it in a bash session from which I then launch notebook with ipython notebook, and I'm able to open and edit .ipynb files normally in the browser.
But I do get some curious behavior:
When exporting from notebook as a .py file, I don't get the control comments documented here but a simpler format, without version number:
# coding: utf-8
# In[ ]:
print "This is a slide"
## Top-level title
### Second-level heading
#### Third-level heading
# This is some `markdown` text.
#
# And some more here.
Any idea what's going on here?
The same format is generated by ipython nbconvert. However, if I start the notebook server with ipython notebook --script (which exports the notebook as a python script every time it is saved), the result contains the nbconvert directives we need to convert back to a notebook!
I had the same problem.
This post helped:
How to load/edit/run/save text files (.py) into an IPython notebook cell?
Basically, we just have to use the following command in the cell. And the .py file has to be in the same directory.
%load filename.py
I'm not sure why notebook doesn't support this natively, but I've concluded that the answer is: It can't be done from the command line or notebook GUI.
Control comments like <markdowncell> can only be interpreted by accessing notebook's API through python, as shown by #CliffordVienna in this answer to my related question.
import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')
Edit: The above method does not work with the current version (v4) of the Notebook API, so I have added this self-answer to show how it's done.
If you only need to import a local file, first use:
sys.path.append(os.getcwd())
to place the .pynb file's directory in sys.path, and then import the local file.

Resources