How to export and preserve linked Jupyter notebooks? - python

I have multiple Jupyter notebooks that are linked to one another such that Notebook1.ipydb contains a link to Notebook2.ipydb with the markdown [Notebook2](Notebook2.ipynb) and vice versa.
When exporting all notebooks to HTML via nbconvert, the link to Notebook2.ipynb is preserved. I would like to change that link to the exported Notebook2.html so the linked HTML files function as a static website.
I tried to detect if I was running in iPython using get_ipython().__class__.__name__, but it executes this code before converting to HTML.
Is there a way to detect a static file to conditionally render the right markdown? Is there another way to preserve linked notebooks?

There's only really two options. One is to link to Notebook2.html in the first place and the other is to create a custom preprocessor for nbconvert.
from nbconvert.preprocessors import Preprocessor
import re
class CustomPreprocessor(Preprocessor):
def preprocess_cell(self, cell, resources, index):
if 'source' in cell and cell.cell_type == "markdown":
cell.source = re.sub(r"\[(.*)\]\(\1\.ipynb\)",r"[\1](\1.html)",cell.source)
return cell, resources
Save this to a file, then add to your nbconvert config file (located at ~/.jupyter/jupyter_nbconvert_config.py or can be generated using the command jupyter nbconvert --generate-config) the line:
c.HTMLExporter.preprocessors = ['CustomPreprocessor.CustomPreprocessor']
This assumes that the custom preprocessor file is named CustomPreprocessor and is located in the same directory as the files you're trying to convert. You could also properly install it as a module too.

Related

How to open .ipynb pages as normal jupyter notebooks

I am learning python using a course. The course material can be found on the links like the following one:
http://faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
I'd like to have the jupyter notebook when I go to the link but it shows the raw python file. How can I export the jupyter notebook from such links?
Thanks in advance for any help.
You can just open an already created jupyter notebook (the file with .ipynb) in a notepad and replace its text from the text in your link.
Steps
Create a totally new jupyter notebook project.
Go to the file location and open it with notepad
Remove all the content from the notepad
Replace it with the content in your link https://faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
Save the notepad and close it.
Open the same file as a notebook using Jupyter notebook or Google Colab
You can copy the raw content and paste to local new file. File extension should
be .ipynb .Then you can open in jupyter lab or notebook.
Go to nbviewer.org, paste in the URL, and press 'Go!'. You'll then be redirected to here which is a page that has the following URL:
https://nbviewer.org/url/faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
At that URL is the notebook rendering you seek. (nbviewer will even display some 'interactive' items such as Plotly plots and animated matplotlib plots backed by frames, examples here and here, respectively.)
Right-clicking the download icon in the upper right side of the notebook rendering there and selecting Save link As... will allow you to save the .ipynb file to your local machine. (You can do similar from the original page link, but there you have to edit the name. No editing of the name necessary this way for your link!)
If you examine the URL generated by the form, you'll note that there is a pattern based on what you provided. And so you could just change the original portion of the link you provided from http://... to https://nbviewer.org/url/... and go to the notebook rendering directly without the step of filling out the form.
If the page had been hosted at GitHub or another repository that MyBinder.org can use, you'd have in the upper right corner an additional icon looking like three rings on the nbviewer rendered page that could be clicked to open it as an active Jupyter notebook right in your browser without needing to login as it would be served vis MyBinder.org. The pages I link to for the Plotly plots and animation have this icon as an option.

Load plain text inline in Markdown in Jupyter

I know you can 'import' text or code in Jupyter notebook cells with %load file_name.ext but that pastes the entirety in the cell. Is there a way to load a blog of text inside a Markdown cell at the desired location?
What I'm looking for is this:
This is some custom text from my current notebook and because I share the same example across multiple notebooks, I'd like to re-use it and not copy-paste it everywhere:
<<<insert contents of some_file.md>>>
And then I can continue with a text that's only visible in this notebook.
That way I do not have to repeat myself and have a single place where I can edit these shared bits of text (or code).

How to display a .gif in a Jupyter Notebook within a Python package

I am writing a small python package for interactive data visualisation. We would like to include gifs with the package so users can run an example and learn how to interact with the figure.
Here is an example structure:
<package>
+-->some_module/
+--> static/
+--> foo.gif
+--> spam.py
+-->examples/
+--> example.ipynb
We have several classes (e.g., spam.py may contain the class Spam), and these classes will have a method .show_gif() which will access the gif (foo.gif) within static and show it to the user.
The code is wrote to be used within a Jupyter notebook. In the package we include some examples (e.g., example.ipynb), these will import spam and then call the .show_gif() method on the relevant class.
Currently we display the gifs using:
from IPython.core.display import Image
Image(filename=path_to_gif)
which works fine when the gif is in a sub-directory of the folder the jupyter notebook is in, but not when the gif is within a sibling directory of the package (as in the above example).
EDIT:
I believe I can access the .gif but cannot display it in a jupyter notebook (see below)
There are similar stack overflow questions (This question) which suggest using importlib.resources module from the standard library. However, they return a BinaryIO instance I don't know how to deal with see:
import some_module.static as static
…
def show_gif(self):
image = pkg_resources.open_binary(static, 'foo.gif')
return Image(data=image)
this throws an error because I am giving Image a wrong type for data. How can I open this .gif and display it in a jupyter notebook?
Use any method to find the path to your gif file and open it for reading; pkg_resources.open_binary() will do, though as this is an actual file on disk there are simpler methods. But now you have an open filehandle, not the content itself-- same as when you call open() on a file. To get the image data, you need to read() from the filehandle.
You then have a bytes object, not a text string. That's not a problem since Image() accepts bytes in the constructor:
with pkg_resources.open_binary(some_module, 'foo.gif') as src:
imagedata = src.read()
image = Image(data=imagedata)

Saving Ipython Notebook with html figures as pdf?

I am using Ipython Notebook and I would like to save the notebook as pdf. When a notebook contains html figures in markdown mode I cannot export them
In example:
<img src='http://draftingmanuals.tpub.com/14262/img/14262_140_2.jpg'>
represents the following:
However, when I download the notebook as PDF via LaTeX (pdf) the result is without the figure:
Is this a bug or can I avoid this somehow?
This is not really a bug, but a known limitation. Actually there are two issues in your example:
the raw html <img> tag gets stripped when the markdown cells are converted by pandoc to latex (see pandoc docu).
you link to a remote image, which is (currently) not downloaded prior to the conversion.
Thus, it is a bit tricky to get what you desire. The first issue may be overcome by means of a custom filter and custom template. For the second, you may need a custom preprocessor.
Alternatively, you could use python with urllib (e.g. Downloading a picture via urllib and python) and matplotlib to display this image. Such embedded images are converted fine.

How can I render HTML in a Jupyter Notebook?

When I browse my file system through a jupyter notebook server I can view and edit HTML and other text files in addition to ipynb files. However I want to view the files as rendered HTML instead of viewing them as a editable HTML.
In other words, how can I make a jupyter notebook server serve static content?
It already does, under /files. For example, when https://tmp39.tmpnb.org/user/IgoeEDdRLpRG/edit/featured/pandas-cookbook/README.md is the URL for editing a file, https://tmp39.tmpnb.org/user/IgoeEDdRLpRG/files/featured/pandas-cookbook/README.md is that file served up as-is (the first two segments of this example file path are specific to tmpnb servers).

Categories

Resources