Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Tips and trics with Jupyter Notebooks

Table of contents

  • Create internal links using the same heading as the one linked to.

  • Autofill happens automagically after writing the hashtag in the parenthesis, but this may miss out on capital letters.

[Including Markdown content from](#Including-Markdown-content-from-file)

Example:

  1. Including Markdown content from

  2. Create filling text on the fly

  3. Exporting to PDF

  4. Interactive graphics

  5. HTML

Including Markdown content from file

# Use IPython.display.Markdown to display the text in ../../data/lorem.txt
from IPython.display import Markdown
with open('../../data/lorem.txt') as f:
    text = f.read()
Markdown(text)

Create filling text on the fly

# Use the lorem package to generate a paragraph.
import lorem as lo
from IPython.display import Markdown
Markdown(lo.paragraph())
Loading...

Exporting to PDF

  • If not installed:

pip install 'nbconvert[webpdf]'

  • First time usage to install Chromium for conversion via HTML instead of LaTeX:

jupyter nbconvert --to webpdf --allow-chromium-download your-notebook-file.ipynb

  • Normal usage:

jupyter nbconvert --to webpdf your-notebook-file.ipynb

  • For general hiding of code in code cells:

jupyter nbconvert --to webpdf --no-input your-notebook-file.ipynb

  • Other export options (may need additional packages installed to work): ‘asciidoc’, ‘custom’, ‘html’, ‘html_ch’, ‘html_embed’, ‘html_toc’, ‘latex’, ‘markdown’, ‘notebook’, ‘pdf’, ‘python’, ‘qtpdf’, ‘qtpng’, ‘rst’, ‘script’, ‘selectLanguage’, ‘slides’, ‘webpdf’.

  • There are also a lot of other customisations available through other parameters, e.g., various templates and themes.

Interactive graphics

  • Basic interactivity is possible directly in Plotly when exporting to HTML.

  • Advanced interactivity needs live running in some Jupyter environment or converting to a dynamic web page using Voilá, Mercury or similar.

  • Plotly’s renderer may need setting for correct compatibility and inclusion of JavaScript.

# The following renders plotly graphs in Jupyter Notebook Jupyter Lab and VS Code formats (first one helps HTML export)
import plotly.io as pio
pio.renderers.default = "notebook+plotly_mimetype+vscode"
# If you set it to some static output like "png", you can export to PDF (requires the kaleido package)
# pio.renderers.default = "png"

# Gapminder dataset of health and wealth stats for different countries
import plotly.express as px
df = px.data.gapminder()

# Animate all years
px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90], 
           width=600, height=400) 
# Make sure to set width and height to avoid scaling issues with PDF export

HTML

  • Markdown supports some HTML, but Markdown commands have no effect inside HTML tags.

  • Basic HTML is preserved also when converting to PDF.

μ=1\mu = 1

Some text over here
ContentMore content
Even more contentLast content $\mu = 1$
Notebook Cell
# Dummy cell to ensure Plotly graphics are shown
import plotly.graph_objects as go
f = go.FigureWidget([go.Scatter(x=[1,1], y=[1,1], mode='markers')])