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.
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.
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)
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)
)