1 of 43

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

2 of 43

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

3 of 43

Agenda

3

  1. Housekeeping
  2. Primer: Using the Django ORM and Python notebooks together ~ 10 mins
  3. Trying it out with Marimo ~25mins
  4. Where to go next ~10mins

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

4 of 43

Housekeeping

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

5 of 43

About the workshop

Aim:

Duration: 50 mins

You will need:

  • A laptop (or share with someone)
  • A working internet connection
  • A modern browser
  • A Github account (ok, not essential, but it really helps)

5

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

6 of 43

About the Green Web Foundation

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

7 of 43

The Green Web Foundation is working towards a fossil-free internet by 2030.

7

GREEN WEB FOUNDATION

8 of 43

Build open source tools, APIs, and libraries

8

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

9 of 43

Create prototypes.

Contribute to open source projects.

Publish open data.

9

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

10 of 43

Talks.

Workshops.

Training.

Consulting.

10

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

11 of 43

Learning objectives

11

  1. Learn how to use Django from a notebook environment

  • Learn to turn a Django query into a simple dashboard

  • Get confidence to try on your own project

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

12 of 43

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

13 of 43

Exploring along

Follow along:

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

14 of 43

Agenda

14

  • Housekeeping
  • Primer: Using the Django ORM and Python notebooks together ~ 10 mins
  • Trying it out with Marimo ~25mins
  • Where to go next ~5mins

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

15 of 43

Primer: Using the Django ORM and Python notebooks together

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

16 of 43

Who here has used Django Shell?

(uv run) manage.py shell

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

17 of 43

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

18 of 43

GREEN WEB FOUNDATION

19 of 43

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

20 of 43

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

21 of 43

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

22 of 43

Easy

How many customers are there?

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

23 of 43

Harder

Who were the 10 customers who placed the most orders?

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

24 of 43

how do I share these results?

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

25 of 43

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

26 of 43

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

27 of 43

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

28 of 43

Agenda

28

  • Housekeeping
  • Primer: Using the Django ORM and Python notebooks together ~ 10 mins
  • Trying it out with Marimo ~25mins
  • Where to go next ~10mins

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

29 of 43

Trying it out with Marimo

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

30 of 43

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

31 of 43

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

32 of 43

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

33 of 43

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

34 of 43

making this concrete

uv run marimo edit \�./notebooks/who-are-our-customers.md

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

35 of 43

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

36 of 43

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

37 of 43

Harder

Which 3 customers placed the largest orders?�

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

38 of 43

follow along

uv run marimo edit \�./notebooks/all-time-biggest-spenders.md

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

39 of 43

Can I have a dashboard plz?

Which 3 customers placed the largest number of orders?�

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

40 of 43

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

41 of 43

Agenda

41

  • Housekeeping
  • Primer: Using the Django ORM and Python notebooks together ~ 10 mins
  • Trying it out with Marimo ~25mins
  • Where to go next ~10mins

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION

42 of 43

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

43 of 43

Thanks! I’ll be at the sprints if you want more

Please take 5 mins to provide the feedback form over lunch.

43

GREEN WEB FOUNDATION

GREEN WEB FOUNDATION