Every time when I try to run a file in the JupiterLab console I get the following message:
ERROR:root:File 'thing.py' not found.
In this case, my file is called thing.py and I try to run it with the trivial run thing.py command in the console. The code is running and it gives me correct results when executed in the console, but I wanted to have it saved, so I put it in a JupiterLab text file and changed the extension to .py instead of .txt. But I get the aforementioned message regardless of which file I try to run. I am new to JupiterLab and admit that I might have missed something important. Every help is much appreciated.
If you're running Jupyterlab you should be able:
to create a new file & paste in your commands
Rename that file to "thing.py"
And then open a console in the same Jupyterlab instance and run that file. Notice that you can see "thing.py" in the file explorer on the left:
Alternatively, you can use the %load magic command in a notebook to dynamically load the code into a notebook's cell.
You might want to understand exactly what a Jupyter Lab file is, and what a Jupyter Lab file is not. The Jupyter Notebooks have the extension, .ipynb.
So anyway, the Jupyter Notebooks are not saved or formatted with python extensions. There are no Jupyter Notebooks or Jupyter Labs ending with the .py extension. That means Jupyter will not recognize an extension having .py, or .txt or .R or etc.... Jupyter opens, reads, and saves files having the .ipynb extension.
Jupyter Notebooks are an open document format based on JSON.
Jupyter can export in a number of different formats. Under the File tab, is the Export feature. The last time I looked there were about 20 different export formats. But there isn't a python or .py export format. A Jupyter file can also be Downloaded. Also under the File tab is the Download feature. This will download a standard text formatted JSON file. JSON files are mostly unreadable unless you've spent years coding JSON.
So there's not much purpose in downloading the Jupyter file unless you are working on a remote server and cannot save your work at that site. And it makes much more sense to save and copy the Jupyter file in its native Jupyter format - that means having the extension, .ipynb . Then just open and use that file on another PC.
Hopefully this should clarify why Jupyter won't open any .py or .txt files.
Related
I'm using imported methods from a .py file in a jupyter notebook and I wish to edit the .py file and use the updated function in the notebook. Currently, when I change a function or class in the .py file I have to close this file in VSCode and restart the notebook kernel to use the methods with the change I just made in the .py file, as if VSCode uses a cached version of the .py file if I don't restart the jupyter kernel. Is there anyway to change this setting? I used to work like this in PyCharm and it worked fine.
You need to enable the auto reload magic in IPython. Try running the following before your code:
from IPython import get_ipython
ip = get_ipython()
ip.magic("reload_ext autoreload") # these will enable module autoreloading
ip.magic("autoreload 2")
In jupyter notebook, because the way of parsing files is based on JSON, the file format saved by default is not .py is .ipynb。 And .ipynb files cannot be simply imported to .py or .ipynb file, which brings great inconvenience to the development. Because in Jupiter notebook, it must be in the default .ipynb can support a series of features, such as automatic completion, console waiting, and so on .py files can only be modified through a text editor, which is very inconvenient.
Because .ipynb can import .py module, so one of the solutions is to convert the .ipynb module to .py file. Create a new cell at the end of the .ipynb file. Adding the following codes:
try:
!jupyter nbconvert --to python file_name.ipynb
except:
pass
# file_ name.ipynb is the file name of the current module
Then a file with the same name will be generated in the current .py file, this module can be used in other .ipynb.
I have written some Jupyter notebooks using original Jupyter notebook web interface. All notebooks are synced nicely in this way.
But now, I would like to edit my notebooks in the VSCode. But I cannot configure syncing notebook file with its python script.
I tried this using jupytext:
created file jupytext in the folder ~/.config
put the next code into this file:
# Always pair ipynb notebooks to py:percent files
default_jupytext_formats = "ipynb,py:percent"
But no effect!
(Update) Can this be achieved, as a first solution, using VSCode Tasks (I am not used tasks yet)?
May be it possible to run the task with jupytext command if the notebook file is opened/saved/modified?
Currently, VSCode does not support such a function. The Jupyter function in VSCode is provided by a Python extension, which supports us to convert between .ipynb files and .py files in VSCode.
.ipynb files to .py files : Export as python script.
.py files to .ipynb files : Right click, "Export Current Python File as Jupyter Notebook"
I have submitted the requirement you described, and we look forward to the realization of this feature. Giuhub link: How to synchronize the jupyter file and python file of VSCode.
I often use jupyter for classes, and use the commmand python -m notebook to open the notebook. The tab that opens up shows an error, and then I have to copy one of the links in the terminal window instead. Is there a way or a command that I can use so that one of those links automatically opens up?
NOTE: It opens the file location (first address in the picture) and fails, and I use the links below to open jupyter. I want the terminal to open the links directly if possible to save time.
This is the error that I get.
jupyter notebook notebook.ipynb
Taken from this documentation.
It shouldn't matter whether you install it using conda or pip.
Or you can just type jupyter notebook which should open up a your file directory in the browser and let you navigate to the notebook file you want to open.
Edit: For this error, it really is hard without knowing the full context of the commands you are running, the directory you are calling from, and where this file you want to open is located. Make sure you are in the folder that contains your notebook file. If not, you should specify the entire path to the notebook file. Finally, does the command jupyter notebook by itself work?
A coworker sent a jupyter notebook converted into html format. I want to edit it, how can I open the html in my jupyter environment?
This is possibly a dublicate of other Question, you might also ask your college to send you the original file, or open the html in your browser and copy the code into a new notebook
You cannot open it directly. It needs to be converted back into an executable .ipynb Jupyter file.
Take a look at this code example to do the conversion:
File-conversion of HTML-published Jupyter Notebook to Executable Jupyter.ipynb file
Nowadays with more and more IPython notebook files (*.ipynb) around, it is very disturbing every time when I want to peek at some notebook I have to open a server for it, and cannot do it in read-only mode. Due to auto-save I can accidentally change the file when reading it if not in read-only mode.
I hope something like this: ipython notebook mynb.ipynb --read-only would work, but sadly it doesn't (although still it creates a server which I don't really want in read-only view). What I really want is to open an ipynb file like a HTML file for reading; currently it seems a missing view of ipynb file, and now the notebook is more like a black-box or near-binary file alone.
(P.S. I am using Linux/Ubuntu.)
When you change the notebook files' permissions, jupyter's auto-save doesn't touch them:
chmod a-w *.ipynb
Then jupyter goes into read-only mode:
Try this ipynb Viewer. This renders ipython notebook as a static web-page.
Also ypu can convert ipyhton notebook to other formats using
ipython nbconvert --to FORMAT notebook.ipynb.
Refer Convert Ipython notebook to other formats. Using this you can convert ipython notebook to HTML.
You may also want to try nteract app (https://nteract.io)
nteract is a desktop application that allows you to develop rich documents that contain prose, executable code (in almost any language!), and images.
Here you can find more detailed review of the app.
It is completely free and very convenient tool by itself and I use it quite often to see other ipynb files if needed and for quick development.
The best I can suggest - unfortunately still a bit verbose - is using nbconvert to create a HTML version of the file, then opening it with your browser. Below are commands to do this (assuming that your browser is set up as the default program to handle .html files). Just replace yournotebook.ipynb with your real Notebook name.
Linux
jupyter nbconvert --to html yournotebook.ipynb --output /tmp/notebook.html &&
xdg-open /tmp/notebook.html
macOS
jupyter nbconvert --to html yournotebook.ipynb --output /tmp/notebook.html &&
open /tmp/notebook.html
Windows
jupyter nbconvert --to html yournotebook.ipynb --output "%TEMP%\notebook.html" && start "" "%TEMP%\notebook.html"
(Note that if you want to do this in bulk, perhaps in a loop in a script, you'll probably want to modify the commands above to not always use the same filename for the HTML file, to avoid the race condition where the HTML file has been overwritten by the time the browser actually gets round to starting to read it.)
Intellij IDEA (which is also available as free open-source community edition) can render ipynb files as well. In fact it also allows to author notebooks, so it's not just a viewer.
It can be used via file type associations or via the command line launcher (e.g. idea foo.pynb).
I use Github Gist for that. You'll need a Github account.
If you specify *.ipynb file extension there for your content copy/paste, it'll detect it and format as html view. You'll be able even sharing it with someone using link if that's interesting. No tools required other than having a browser.
I've found some glitches in formatting though, mostly with output plots, but it's reasonably rare.
Random example here: https://gist.github.com/Clockware/aa7e01722579841d5888ca83385a5f1d
I was able to read .ipynb files as html in visual code. You would need a python plugin for it which visual code auto detects. Fairly straight forward after that.
This most convenient method to take a peek at jupyter notebooks is by using nb-viewer, with this utility, you will be able to open notebooks (in read-only mode) with a double click.