Tips and Tricks

Here we cover some miscellaneous tips that may be appealing to certain niche use-cases.

Executing markdown files during sphinx-build

The tool called jupytext allows the author to export a Jupyter Notebook into a Markdown file and vice-versa. This current text notebook was initially generated using:

$ jupytext --to md:myst tips-and-tricks.ipynb

It is also possible to open such text notebooks in JupyterLab by running

$ jupytext-config set-default-viewer

See also

Learn more about using text notebooks here:

Embedding video

See the code below to see how a video can be embedded using an iframe.

Hide code cell source

%%HTML
<div align="center">
<!-- The following is the snippet exported directly from YouTube, as is -->
    <iframe 
        width="560"
        height="315"
        src="https://www.youtube.com/embed/_UOy1c_gz3g"
        title="YouTube video player" 
        frameborder="0"
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
        referrerpolicy="strict-origin-when-cross-origin" 
        allowfullscreen>
    </iframe>
</div>

Tip

Alternative method to embed videos

# from IPython.display import Video  # for a generic source
from IPython.display import YouTubeVideo  

YouTubeVideo('_UOy1c_gz3g', width=560, height=315)

It is also possible to embed local videos using IPython.display.Video.

Interactive visualization

You can embed visualizations in your content using executable code cells using the {code-cell} ipython3 directive.

First we prepare two dataframes named data and true_sine in the cell below.

Hide code cell source

import numpy as np
import pandas as pd

np.random.seed(42)
x = np.linspace(0, 10, 100)

data = pd.DataFrame({
    'x': np.tile(x, 3),
    'y': np.concatenate([
        np.sin(x) + np.random.normal(0, 0.1, 100),
        np.sin(x) + np.random.normal(0, 0.1, 100),
        np.sin(x) + np.random.normal(0, 0.1, 100)
    ]),
    'experiment': ['A'] * 100 + ['B'] * 100 + ['C'] * 100
})

true_sine = pd.DataFrame({'x': x, 'y': np.sin(x)})

Altair

import altair as alt

base = alt.Chart(true_sine).mark_line(color='black').encode(x='x', y='y')
points = alt.Chart(data).mark_circle(opacity=0.5).encode(x='x', y='y', color='experiment')

(base + points).properties(width=600, height=300).interactive()

Bokeh

from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
from bokeh.transform import factor_cmap

output_notebook()


true_source = ColumnDataSource(true_sine)
exp_source = ColumnDataSource(data)
color_map = factor_cmap('experiment', 'Category10_3', ['A', 'B', 'C'])

p = figure(title="Sine waves with noise", width=600, height=300)
p.line(
    x='x', y='y', source=true_source,
    line_width=2, color='black', legend_label='True sin(x)'
)
p.scatter(
    x='x', y='y', source=exp_source,
    size=4, color=color_map, alpha=0.5, legend_group='experiment'
)
show(p)
Loading BokehJS ...

Holoviz and hvplot

%%capture --no-display
import hvplot.pandas

(
    true_sine.hvplot.line(x='x', y='y', color='black')
    * data.hvplot.scatter(x='x', y='y', by='experiment', alpha=0.5)
)