Python Interactive Code Cells

Demos showing interactive use cases of Python in Your Quarto Documents

Author

James Balamuta

Published

August 8, 2023

Welcome to the world of interactive code cells, unlocked by the quarto-pyodide extension. These cells allow you to run Python code directly within your Quarto HTML documents, enabling real-time computations and more. Let’s explore the impressive capabilities pyodide offers. pyodide-enabled code cell are established by using {pyodide-python} in a Quarto HTML document.

Creating pyodide-Enabled Code Cells

To create a pyodide-enabled code cell, simply use the {pyodide-python} tag in your Quarto HTML document, like this:

For example, the code cell above, powered by pyodide, was generated by entering the following into the Quarto document:

```{pyodide-python}
1 + 1
```

Sample Use Cases

Now, let’s delve into some practical scenarios where interactive code cells shine.

Sample Calculations

Let’s start off with a quick calculation

Strings

Viewing string data

Retrieving prior objects

Checking string length

Line-by-line Execution

In this section, we’ll explore the built-in keyboard shortcuts for executing code within the interactive code cell. You can run either the selected code or specific lines or the entire cell with the following keyboard shortcuts:

  • Run selected code:
    • macOS: + ↩︎/Return
    • Windows/Linux: Ctrl + ↩︎/Enter
  • To run the entire code cell, you can simply click the “Run code” button, or use the keyboard shortcut:
    • Shift + ↩︎

Feel free to try it out in the following code cell:

By using these shortcuts, you can run code conveniently and efficiently. This practice can also help you become familiar with keyboard shortcuts when transitioning to integrated development environments (IDEs) like RStudio or Visual Studio Code with Python.

Preventing Modifications to Code

Code cells can be locked to their initial state by specifying #| read-only: true.

```{pyodide-python}
#| read-only: true
1 + 1
```

Define and Call Functions

Functions can be defined in one cell and called.

Similarly, they persist to other cells.

Load a package

There are two types of Python packages that will work within an interactive cell.

  • pure Python packages
    • denoted by *py3-none-any.whl on PyPI
  • Python packages compiled for Pyodide
    • denoted by *-cp310-cp310-emscripten_3_1_27_wasm32.whl that require a specific Python and Emscripten versions

The latter option makes up part of the Pyodide “core” or “base” set of Python packages.

Important

Not all functionality of a Python package may be available in the browser due to limitations or different versions being present.

Loading a Pyodide core package

For packages that are part of Pyodide core, we’ve enabled dynamic package detection to handle importing packages into the environment. The dynamic part comes from detecting whether a Python package is being used through an import statement and automatically taking care of the installation process behind the scenes.

Note

Importing a package for the first time will require more time. Subsequent import statements will be resolve quicker.

Loading non-core Pyodide Python Packages

In the above example, everything just worked as pandas in available as part of Pyodide’s built-in packages. However, if we need a package that is not part of the built-in list, then there either needs to be a pure Python wheel (no compiled code present) or a specially compiled version of the Package for Python.

In this case, we can install the palmerpenguins package from PyPI with:

Then, we have:

Graphing

We provide support for generating graphs through the interactive HTML5 backend, e.g. module://matplotlib_pyodide.html5_canvas_backend. At the end of each graph call, you must include a plt.show() call for the graph to render.

External Data

Interactive cells also allow for a limited subset of operations when working with external data. For example, we can use Pandas’ read_csv() function to ingest data from a URL.

Important

This Quarto extension is open source software and is not affiliated with Posit, Quarto, or Pyodide. The extension is at best a community effort to simplify the integration of Pyodide inside of Quarto generated documents.