Jupyter prelude
Keywords
python, ibis, pandas, matplotlib, jupyter
uv
# This will create a new Python environment in the current directory
uv init --app
# This will add the specified packages to the environment
uv add "ibis-framework[duckdb,geospatial]" ipykernel panel hvplot holoviews matplotlib pandas param lonboard folium
prelude
Copy the following to the top of your Jupyter Notebook to load the prelude:
################################################################################
# autoreload all modules every time before executing the Python code
%reload_ext autoreload
%autoreload 2
################################################################################
from IPython.core.interactiveshell import InteractiveShell
# `ast_node_interactivity` is a setting that determines how the return value of the last line in a cell is displayed
# with `last_expr_or_assign`, the return value of the last expression is displayed unless it is assigned to a variable
= "last_expr_or_assign"
InteractiveShell.ast_node_interactivity
################################################################################
import pandas as pd
# `copy_on_write` is a performance improvement
# This will be the default in a future version of pandas
# Refer to https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html
= True
pd.options.mode.copy_on_write = True
pd.options.future.no_silent_downcasting
################################################################################
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline
"agg")
mpl.use(
# `constrained_layout` helps avoid overlapping elements
# Refer to https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html
'figure.constrained_layout.use'] = True
mpl.pyplot.rcParams[
# helper function to create a grid layout for subplots
def make_grid(labels=None, ncols=None, nrows=None, placeholder="."):
"""
Create a grid layout suitable for matplotlib's subplot_mosaic.
If `labels` is None, auto-generate labels like 'A1', 'A2', ..., 'B1', 'B2', etc.
Args:
labels (list of str or None): Subplot labels. If None, generate from ncols and nrows.
ncols (int or None): Number of columns in the grid (required).
nrows (int or None): Number of rows (required if labels is None).
placeholder (str): Placeholder for empty grid cells.
Returns:
list of list of str: A 2D list representing the grid layout.
"""
import string
import itertools
if ncols is None or ncols <= 0:
raise ValueError("ncols must be a positive integer")
if labels is None:
if nrows is None:
raise ValueError("If labels is None, nrows must be specified.")
def _excel_style_names():
for size in itertools.count(1):
for chars in itertools.product(string.ascii_uppercase, repeat=size):
yield "".join(chars)
= itertools.islice(_excel_style_names(), nrows)
row_names = [f"{row}{col + 1}" for row in row_names for col in range(ncols)]
labels
= [labels[i : i + ncols] for i in range(0, len(labels), ncols)]
grid
if len(grid[-1]) < ncols:
-1].extend([placeholder] * (ncols - len(grid[-1])))
grid[
return grid
Reuse
Citation
BibTeX citation:
@online{krishnamurthy2024,
author = {Krishnamurthy, Dheepak},
title = {Jupyter Prelude},
date = {2024-12-16},
url = {https://kdheepak.com/blog/jupyter-prelude/},
langid = {en}
}
For attribution, please cite this work as:
D.
Krishnamurthy, “Jupyter prelude,” Dec. 16, 2024. https://kdheepak.com/blog/jupyter-prelude/.