Help › Work in notebooks
Run Python in a notebook
Run Python cells in a notebook through a Jupyter kernel on this computer, hand query results to pandas, and query a DataFrame from a scratch cell.
Querlyn 0.1.0 is not released yet. This help describes it ahead of release.
A notebook can hold Python cells beside its query cells. They run in a Jupyter kernel on this computer, as Jupyter's cells do: a query cell's result reaches them as a pandas DataFrame, and the DataFrame a Python cell ends with can be queried by a scratch cell. Querlyn saves Python cells so that Jupyter and Visual Studio Code run them too, and runs the Python cells of the Jupyter notebooks it opens.
Install a kernel
Querlyn does not include Python. Its Python cells run in a Jupyter kernel
that is installed on this computer: a Python with the ipykernel package.
Query results reach Python only when the same Python also has pandas and
pyarrow. Installing them is up to you, in the Python environment you
want the cells to use; for example, with that environment active:
python3 -m pip install ipykernel pandas pyarrow
On Windows the command usually starts with python instead of python3.
Install as well whatever your cells import, such as matplotlib for
plots.
Querlyn looks for kernels in three places:
- kernels installed for Jupyter, in the folders Jupyter reads them from;
- the Python set under Settings › Tools (
python_pathin the settings file), when it hasipykernel; python3on the path (pythonon Windows), when it hasipykernel.
Add Python cells
- (Python in the notebook's toolbar and under the last cell) adds a Python cell under the selected one.
- makes the selected cell a Python cell. It keeps its text and loses its results.
A Python cell's editor colors Python. Completion, diagnostics and formatting are for query cells only. A note at the top of the cell says that it runs outside Querlyn's SQL safety checks and how large a result may be on its way to or from Python; point at it to read the whole sentence.
Run Python cells
Python cells run with the same commands as query cells: (⌘↩ on macOS or Ctrl+Enter on Windows and Linux), , , and the others. Query and Python cells run in the notebook's order, one after another, and the run stops at the first cell that fails.
The kernel starts the first time a Python cell runs, never when the notebook opens. Its cells share it, as in Jupyter: a variable one cell sets is there for the next, until the kernel restarts.
A cell's output shows under it, as it comes:
- what it prints, standard error in its own color (the last 60 lines of each output);
- an exception, with its class, its message and its traceback;
- pictures: PNG, JPEG and SVG, so a matplotlib plot shows as in Jupyter;
- a DataFrame or Series in the result grid, with its index, where you can sort, filter, copy and export it as any result;
- other values as text. HTML is shown as its text (a table as a grid), never drawn as a page and never run; JavaScript and widgets are kept in the file, not shown.
A cell cannot ask for input: input() fails with an error.
The file keeps every output within the notebook's bound, so the notebook reads the same in Jupyter.
Use query results in Python
Every query cell's result has a name, such as cell_2 (see
Name a result). A Python cell that uses
that name gets the result as a pandas DataFrame in a variable of that
name:
revenue.sort_values("share", ascending=False).head(10)
Before the cell runs, each result its code names is handed to the kernel,
unless the kernel already has it from the same run of its cell. The rows
handed over are those a scratch cell
would read: every row when the query cell ran in the same run, otherwise
the rows the cell holds. querlyn.results holds every result the kernel
received, by name.
Values arrive as exactly as pandas can hold them:
| In the result | In the DataFrame |
|---|---|
| Exact decimals | decimal.Decimal values, never floating point |
| Integers | Integers; a column with NULLs stays integer |
| Timestamps with a time zone | The same instants, in UTC |
| UUIDs, JSON, intervals, times with an offset, enums | Their text |
| NULL | Missing (<NA> or None) |
Masked columns arrive empty: their values never reach Python.
A result travels in memory only, as Arrow data over the kernel's own connection: nothing is written to a file on the way, Production's results included. A result may be up to 64 MB as Arrow data; a larger one is not handed over, and the cell says so. Filter or aggregate it in SQL first.
Use the notebook's parameters in Python
A notebook's parameters reach its Python cells as
papermill gives them: before a run's first Python cell, Querlyn sets each
parameter as a Python variable of its name, with the value the run uses
(the form's, or those of
). So region in a
Python cell is the same value :region binds in a query cell.
| Parameter | In Python |
|---|---|
| Text | str |
| Number | int for a whole number, float for a fraction, decimal.Decimal when a float could not hold every digit |
| Date | datetime.date |
| Date range | a tuple of two datetime.dates, and <name>_start and <name>_end beside it |
| True or false | True or False |
| No value | None |
| A list the cell assigns | a list of its values |
Values are set as values, never as code: whatever text a value holds, Python receives that text. A parameter whose value Querlyn cannot read (an expression in the parameters cell) is not set.
Use a DataFrame in a query
The DataFrame or Series a Python cell ends with (its last line's value)
is the cell's result, named like a query cell's result: cell_4, shown at
the top of the cell, renamed with
. A scratch cell that
names it reads it as a table:
SELECT category, per_order
FROM cell_4
ORDER BY per_order DESC
Its index becomes columns, and a column of mixed values becomes text.
Other Python cells have it in querlyn.results. It comes back the same
way a result goes, in memory as Arrow data of at most 64 MB.
Python and out-of-date results
A Python cell depends on every cell above it, since its kernel holds what they made. Its result is marked out of date like a query's: when its text changes, when a cell above it changes or runs again, and when the kernel restarts. runs Python cells as it runs query cells, but a Python cell's result is never reused: Querlyn cannot know what its code does. See Keep a notebook's results up to date.
Control the kernel
A notebook with Python cells shows its kernel in the toolbar: its name, and whether it is Not started, Starting…, Idle, Busy or Stopped. Beside it:
-
(I I in command mode) stops the code
that runs: Python raises
KeyboardInterruptin the cell, and the run stops there. On Windows a kernel may not stop for it; Restart Kernel stops it in every case. - (0 0 in command mode) starts a new kernel, without the old one's variables.
- stops it; the next Python cell that runs starts it again.
Closing the notebook, or quitting Querlyn, shuts its kernel down. The kernel runs in the folder of the notebook's file, so a cell can read the files beside it by their names.
Choose a kernel
(also the kernel's name
in the toolbar) lists the kernels and Pythons Querlyn found, each with
where it is. One that cannot run cells says why, and one without pandas
and pyarrow says that query results cannot reach its cells. Look
Again looks again, after you install something. Use Kernel runs the
notebook's Python cells on the selected kernel from then on.
The notebook's file names its kernel, so the notebook runs on the same kernel when you open it again. Until you choose one, a notebook runs on the kernel its file names, or else on the first one found. When the file's kernel is not installed here, the notebook runs on the first usable kernel and says so once, when that kernel starts ("Kernel not installed: Python 3.11 (analytics). The cells run on Python 3 (ipykernel).") The chooser says so too, and the kernel's description in the toolbar keeps saying it.
Notebooks from elsewhere
A notebook from another program or another person is not trusted until you say so (see Open notebooks from others), and its kernel does not start while it is not. Running one of its Python cells asks Trust this notebook to run its Python? first: its code runs on this computer with your permissions, as any program you run does. Trust and Run trusts the notebook and runs the cells; Cancel runs nothing.
What Querlyn does not check
Querlyn's SQL safety checks read SQL; they cannot read Python. A Python cell can do anything a program you run can do, so:
- on a Production connection, the one question a run asks lists its Python cells, saying that they run outside Querlyn's SQL safety checks;
- Querlyn gives the kernel no connection and no password, token or key: it receives only the query results its code names.
Help for Querlyn 0.1.0. All help topics