Workshop:
�Sharing the joys of the Django ORM with Python Notebooks
GREENWEB.ORG
Chris
Adams
2025.04.24
Djangocon Europe 2025
1
GREEN WEB FOUNDATION
Hello, I’m Chris!
My background:
Loco2 - Low CO2 Travel in Europe by train
A.M.E.E (Avoid Mass Extinction Engine) - CO2 calcs as an API
Green Software Foundation - Policy chair
Branch Magazine - editor
Green Web Foundation - make the web green
Environment Variables podcast - co-host
Django Fundraising Working Group - newest member!
2
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Agenda
3
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Housekeeping
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
About the workshop
Aim:
Duration: 50 mins
You will need:
5
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
About the Green Web Foundation
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
The Green Web Foundation is working towards a fossil-free internet by 2030.
7
“
GREEN WEB FOUNDATION
Build open source tools, APIs, and libraries
8
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Create prototypes.
Contribute to open source projects.
Publish open data.
9
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Talks.
Workshops.
Training.
Consulting.
10
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Learning objectives
11
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Accessing this deck
12
This deck is online.
�Point a camera at the QR
code:
https://www.thegreenwebfoundation.org/events/djangocon-europe-2025
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Exploring along
Follow along:
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Agenda
14
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Primer: Using the Django ORM and Python notebooks together
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Who here has used Django Shell?
(uv run) manage.py shell
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Exploring a sample app & db - Northwind
Commonly used training database, to simulate an e-commerce company.
Lots of tables, and models.
Data already available as a handy fixture for us to mess around with.
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Don’t panic!
Don’t worry! We’re only looking at a couple of tables today!
Customers, and Orders
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Customers
class Customer(models.Model):
customer_id = models.CharField("Customer ID", primary_key=True)
company_name = models.CharField("Company name, max_length=40)
contact_name = models.CharField(
"Contact name", max_length=30, blank=True, null=True
)
(snip)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Orders
class Order(models.Model):
order_id = models.AutoField(_("Order ID"), primary_key=True)
customer = models.ForeignKey( Customer, blank=True, null=True,
on_delete=models.CASCADE
)
employee = models.ForeignKey(Employee, blank=True, null=True,
on_delete=models.CASCADE,
)
order_date = models.DateField(
“Order date", blank=True, null=True, db_index=True)
(snip)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Easy
How many customers are there?
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Harder
Who were the 10 customers who placed the most orders?
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
how do I share these results?
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
two main options for notebooks in 2025
dj-notebook - use jupyter (common data science tool) to access the Django ORM
marimo - new, slightly more pythonic project, that addresses a few common pain points of jupyter notebooks
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
DJ notebook
order of cells matters (hidden state)
can run multiple languages, not just python
more established, bigger ecosystem
saves as .ipynb files (json files)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Marimo
order of cells does not matter
mainly python focussed
fewer moving parts
saves as .py or .md files
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Agenda
28
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Trying it out with Marimo
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
What is Marimo?
“marimo is an open-source reactive notebook for Python — reproducible, git-friendly, SQL built-in, executable as a script, and shareable as an app.”
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
how to run it - uv run marimo
Usage: marimo [OPTIONS] COMMAND [ARGS]...
Welcome to marimo!
Getting started:
* marimo tutorial intro
Example usage:
* marimo edit create or edit notebooks
* marimo edit notebook.py create or edit a notebook called notebook.py
* marimo run notebook.py run a notebook as a read-only app
* marimo tutorial --help list tutorials
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
how to run it
uv run marimo edit /path/to/file
(opens that notebook. Can be python or markdown)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
making this concrete
uv run marimo edit \�./notebooks/who-are-our-customers.md
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
how this is working - django helper
def setup_django_for_marimo(main_project_path=None, set_async_unsafe=True):
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
if not main_project_path:
main_project_path = find_django_project()
# if we want to run queries a notebook environment we need to set this
if set_async_unsafe:
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "True"
if main_project_path not in sys.path:
sys.path.append(main_project_path)
return django.setup()
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Easy
Which city has the most customers?
(Hint: Update the widgets to filter listings by city, instead of country)
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Harder
Which 3 customers placed the largest orders?�
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
follow along
uv run marimo edit \�./notebooks/all-time-biggest-spenders.md
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Can I have a dashboard plz?
Which 3 customers placed the largest number of orders?�
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
follow along
uv run marimo edit \�./notebooks/customer-order-dashboard.py
�# run in dashboard more
uv run marimo run \�./notebooks/customer-order-dashboard.py
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Agenda
41
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
There is LOADS to Marimo.
Play with the features some more:
�uv run marimo tutorial
�Main website - check the gallery!�https://marimo.io/
Excellent videos�https://www.youtube.com/@marimo-team
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION
Thanks! I’ll be at the sprints if you want more
Please take 5 mins to provide the feedback form over lunch.
www.linkedin.com/in/mrchrisadams
mastodon.social/@mrchrisadams�
climateAction.tech - a supportive climate-tech community
43
GREEN WEB FOUNDATION
GREEN WEB FOUNDATION