How to transfer en bloc all the cells of a Jupyter Notebook? - python

So I found this great visualization of Newton's unconstrained optimization on a Jupyter Notebook within Louis Tiao's public account, and I want to run it on my laptop.
With other platforms, I'd be able to just copy and paste (including the annotations), and get it ready to "play". But with Notebook, I have to deal with multiple cells, and copy and paste each one separately, and in order.
Is there a more expeditious way of transferring the code?

A jupyter notebook is stored in a file with a .ipynb extension. The internals of the file a specially formatted text called json (or similar to it).
Right click the link and save as name.ipynb (it defaults to this in windows on chrome) and choose a location to save it. The best location is one where you have all your notebooks by default.
Then run jupyter and open the file.

Related

Jupyter Notebook PDF "shifting" sections to the right and making them smaller

When converting my Notebook into a PDF, I am finding they are shifting in a funny way, and I have no clue how to fix this. Attached are two images, one of the notebook and the other of a PDF saved via pyppeteer, I am running the notebook locally if that makes any difference.
Also attempting all other methods of saving the notebook as a PDF have the same consequence and also remove the images, seemingly haven lost access to the file directory where I am sourcing them from.
PDF
More of the PDF
Original is all in line correctly like this

How to export colab notebooks to pdf file

this is my first time asking a question on this forum, so, any tip or suggestion is highly appreciated!
As for the question itself, I have already seen many discussions on how to export a Colab notebook as a pdf, however I would like to ask more specifically if there is any way of doing it that can preserve the output of executed code (e.g.: I would like tables made from dataframe in pandas to be exported as they were printed on the notebook and not like a bunch of strings).
I think the easier method is you can use browser print functionality.
for most browser it's shortcut should be ctrl + p
and the the harder method is that you can download ipynb file to your machine and then use jupyter notebook to do this
for this to work you should install notebook-as-pdf pip package and then you need to use this command in your command-line or terminal
pyppeteer-install
after that you are all set, so now you can open your ipynb with jupyter notebook and you should find "PDF via HTML(pdf)" option in "download as" section of file menu
in other word it should be here:
file > download as > PDF via HTML(pdf)
if you want more details on this use this and this.

How can I create and execute a cell in Jupyter Notebook server from a separate process (e.g., an editor, vim)?

I want to call an api on a running jupyter notebook to create or edit an existing cell, and to run it. When this is done, the browser page should also update, to show both the contents of the cell, and the output.
The intended use case for this api is a plugin for the vim editor that will take highlighted lines and execute them. The motivation for using jupyter notebook is that will allow visualization of plots and images using matplotlib, even though both vim and the notebook will be running on a remote server. The user would have a browser page open to the notebook, and separately a terminal open potentially that is ssh'ed to the machine and editing python code.
VSCode does exactly this in its python interactive mode, but I prefer to use the vim editor natively than switch to vscode simply to be able to do this.
There are several plugins being actively developed for this, and without having used them, it seems only vimpyter interacts with jupyter on the browser. Others like jupyter-vim and vim-ipython open the notebook inside a vim buffer.
If you can live without inline plots I find confortable having a neoterm terminal buffer where I can send lines to and from IPython.
You could use vscode's built in jupyter notebook support to convert the notebook into a python file
this is not an exact answer, but I've written a couple Jupyter plugins that may be helpful to you in this endeavor, JupyterGraffiti and JupyterTerminals. Graffiti has an API you can call to execute some of its functions, and it supports inline terminals you can control. JupyterTerminals provides only those inline terminals, but also has buttons you can add to control them. You could start and run vim inside these terminals. (If it's useful an API could be added to the JupyterTerminals plugin like Graffiti has.)

How to work with .ipynb files without launching the Jupyter Notebook server?

I'm starting to work more with Jupyter notebooks, and am really starting to like it. However, I find it difficult to use it with my particular setup.
I have a workstation for running all the notebooks, but for a large part of my day I'm on-the-go with a space-constrained laptop with no power outlets. I'd like to be able to edit (but not run) these notebooks without installing and running the full Jupyter server backend, which I imagine would suck up a lot of power.
My question is: Is it possible for me to edit (and not run) notebooks without running the Jupyter server?
You could use one of the following options
1. ipynb-py-convert
With this module you can do a conversion from .py to .ipynb and vice-versa:
ipynb-py-convert ~/name_of_notebook.ipynb ~/name_of_notebook.py
where according to the documentation the cells are left as they are. To get back a jupyter notebook
ipynb-py-convert ~/name_of_notebook.py ~/name_of_notebook.ipynb
2. Ipython
However, you could also do a conversion to .py when you want to work it with an editor like VS Code or Sublime Text after you have download your .ipynb file with ipython:
ipython nbconvert --to python name_of_your_notebook.ipynb
As I was asking this question, I had opened the notebook locally in Visual Studio Code, but the preview was just the raw text representation of the notebook, so I had assumed that it needed the backend to run.
However, I was about to press submit on the question when I checked back in on it, and the notebook showed up just fine. So one solution is to open it in VS Code and wait a little bit.

Can you stop PyCharm from automatically closing script files when you click out of the program?

I am having a problem with PyCharm (Community Edition) in that, when I open a .py file in the program, I can happily read and write in the file as usual, however when I click out of PyCharm (to look my emails for example) and then click back into it to carry on with my code, the file automatically closes and the project tree structure collapses (so I have to re-open it every time).
So far I have tried changing the tab limit to a high number, but this doesn't seem to help (and it shouldn't be related, since this happens when I open just one file).
I had the same issue before. I'm assuming you're connected to a network shared folder via UNC path (e.g. \\foo\bar\)?
If so, it is not currently supported. You'll need to map your network folder and give it a Drive letter. Then load up your project using the mapped drive, and it'll work like a charm.
If that's not the scenario though, please give us more information.

Categories

Resources