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:
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)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi mattis blandit dolor, et maximus mauris condimentum sit amet. Nam ac scelerisque sapien. Vivamus sagittis nibh dolor, fringilla maximus quam tincidunt a. Morbi sodales augue eget ex aliquet congue vitae et ante. Nulla mollis felis quis ex molestie, vitae viverra magna sollicitudin. Integer scelerisque semper laoreet. Mauris vestibulum mauris massa, quis egestas tellus sollicitudin eu.
Sed mattis efficitur est, a tincidunt orci gravida in. Vestibulum commodo, purus et malesuada commodo, dui metus commodo dolor, vel laoreet diam purus in erat. Vivamus faucibus, est ac pulvinar volutpat, leo purus luctus neque, sit amet fermentum orci metus nec nulla. Nulla efficitur justo sed vehicula viverra. Donec id lorem porttitor, porta felis in, feugiat lorem. Etiam id eros eget orci aliquet molestie eget a justo. Maecenas tincidunt velit sit amet quam hendrerit dapibus. Integer pellentesque sollicitudin interdum.
Nullam enim leo, iaculis sed porta at, imperdiet in ante. Suspendisse porttitor vestibulum lectus eget interdum. Curabitur convallis sodales convallis. Aliquam cursus non velit vitae sollicitudin. Sed eu sagittis metus. Curabitur convallis magna ornare ipsum pulvinar tristique. In fermentum non orci quis aliquam.
Donec aliquet, lacus eget pharetra tincidunt, mi quam ultrices eros, vitae dictum metus sapien quis mi. Curabitur vel rutrum tellus, et aliquet libero. Donec eu purus sed lorem sagittis feugiat. Nullam a euismod sem, eu tincidunt libero. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed ut leo enim. Aliquam erat volutpat. Duis ornare malesuada vestibulum. In non luctus purus, quis fringilla mi.
Quisque consectetur ultrices nibh, nec efficitur est. Praesent sit amet nulla sit amet odio viverra scelerisque. Praesent in nunc est. Proin tristique quam a magna auctor hendrerit. Integer massa quam, ullamcorper sit amet metus auctor, finibus pretium augue. Phasellus hendrerit quis leo a pulvinar. Mauris viverra mi ac luctus tempus. Phasellus a nulla sodales, finibus sem a, commodo nisl. Curabitur a sagittis enim. Nam maximus feugiat massa.
Create filling text on the fly#
# Use the lipsum package to generate 100 words.
import lipsum as lp
from IPython.display import Markdown
Markdown(lp.generate_words(100))
Constituto autem illo, de quo ante diximus, quod honestum esset, id esse solum bonum, intellegi necesse est pluris id, quod honestum sit, aestimandum esse quam illa media, quae ex eo comparentur. stultitiam autem et timiditatem et iniustitiam et intemperantiam cum dicimus esse fugiendas propter eas res, quae ex ipsis eveniant, non ita dicimus, ut cum illo, quod positum est, solum id esse malum, quod turpe sit, haec pugnare videatur oratio, propterea quod ea non ad corporis incommodum referuntur, sed ad turpes actiones, quae oriuntur e vitiis. quas enim kakw Graeci appellant, vitia malo quam malitias nominare. Ne tu, inquam, Cato!
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.
Installing the python package nbconvert-theme-pale-sand-navy and using –template pale-sand-navy is an example.
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.
\(\mu = 1\)
Content | More content |
Even more content | Last content $\mu = 1$ |
Show code cell content
# 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')])