Skip to content

Save a query so someone else can run it

Draft: not yet validated

The code on this page was run inside MESA JupyterLab on 2026-11-16 (pandas 3.1.1, DuckDB 2.0.0) by the site's output check, in a terminal there, and gave the values shown. The steps were also followed once in a notebook there by the AI assistant that drafted them. No person has yet followed the steps, so a menu or a screen may differ. Tell us what you find.

A query typed into a notebook cell is easy to change and hard to hand over. In a file of its own it has a name, it can be read without opening the notebook, and whoever runs it runs the same text you did.

You will: have a file, flagged_readings.sql, beside your notebook, and a cell that runs the query from that file.

Time: 5 to 10 minutes. You need: the notebook from Load a file from your Data Store into a notebook, open in MESA JupyterLab, with its first cell run.

Steps

  1. Write the query as text. Start it with comment lines, which begin with --, that say what it returns and which file it reads:

    query = """-- Readings the sensor marked suspect, oldest first.
    -- Needs readings.csv in the folder this is run from.
    SELECT gauge_id, date, value
    FROM 'readings.csv'
    WHERE flag = 'S'
    ORDER BY date;
    """
    

    You should see: no output.

  2. Save the text to a file beside the notebook:

    from pathlib import Path
    
    saved = Path("flagged_readings.sql").write_text(query)
    

    You should see: no output, and flagged_readings.sql in the file browser.

  3. Run the query from the file, not from the variable:

    import duckdb
    
    duckdb.sql(Path("flagged_readings.sql").read_text())
    

    You should see: the two flagged readings. In a notebook, DuckDB draws the result as a table, with each column's type under its name; the text below holds the same rows.

    ┌──────────┬────────────┬────────┐
    │ gauge_id │    date    │ value  │
    │ varchar  │    date    │ double │
    ├──────────┼────────────┼────────┤
    │ G05      │ 2026-07-09 │  180.0 │
    │ G03      │ 2026-07-18 │    0.0 │
    └──────────┴────────────┴────────┘
    
  4. Double-click flagged_readings.sql in the file browser.

    You should see: the query in an editor tab, with your two comment lines at the top. This is what a colleague reads first.

  5. From now on, change the query in the file, save it, and run the cell from step 3 again. Delete the cells from steps 1 and 2 once the file exists, so that the notebook holds one copy of the query and not two.

    You should see: the cell from step 3 still giving the same two rows.

Check it worked

The folder holds flagged_readings.sql, and the one cell that reads it returns two rows. Someone with the folder can run that cell, or read the file, without asking you what the query was.

If it didn't

IO Error: No files found that match the pattern "readings.csv". The query names its data file without a folder, so it must be run from the folder that holds the file. Open the notebook from mesa-practice, where readings.csv is, and run the cell again.

Next

Not yet validated by a person in MESA JupyterLab. Drafted with AI assistance on 2026-11-15; followed once there on 2026-11-16 (pandas 3.1.1, DuckDB 2.0.0).

Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.