Python cells

Creating a Python cell

Create a Python cell by either:

  • Using the Y keyboard shortcut to place a new cell

  • Selecting the Python cell option from the control bar

  • Selecting the Add Python cell option when referencing a cell from the + icon that appears when the cell is selected.

How Python cells work

Python cells work very similarly to SQL cells, consisting of a text input area and an output area. Python cells are reactive just like all other cells, and their relationships are indicated by the same connector lines.

In Count, Python cells are executed locally in your browser using a version of Python that has been developed to work on the web. When you first execute a Python cell, the Python environment is downloaded and started, which may take a few seconds the first time.

Referencing other cells using the cells variable

In Python cells, there is a special global cells variable that contains the results of other cells formatted as pandas DataFrames. Access cell results using keys or attributes on this object:

When importing non-Python cells as DataFrames, text columns may be imported as a categorical series if the cardinality of the column is low. This lowers the memory usage of the column, which is helpful when importing large result sets.

Most operations work the same way on categorical vs non-categorical columns, but if a non-categorical form is required, then use the astype method:

df['column'] = df['column'].astype('object')

Referencing Python variables

All variables defined at the root scope of a Python cell are global, and can be accessed in any other Python cell. Count will detect relationships between Python cells based on the variables they reference, and add connector lines automatically.

Because Count executes cells reactively, it's possible to accidentally create circular dependencies. In this case, like other cells, Python cells will display an error and refuse to execute:

Referencing Python cells from DuckDB

The last expression in a Python cell is special, and becomes the single output of that cell. If this output can be represented as a table, it can be queried by local DuckDB cells too:

Python cell outputs

As Python is a more expressive language than SQL, it is able to output more data types:

  • Table output - if the final expression of the cell is representable as a table

  • Image output - if the final expression of the cell is a PNG-formatted image bytes object

  • Logs output - if the cell has printed anything during its execution

The output type defaults to Automatic, which can be overridden from the Output type button above the cell.

Interactivity

Because Python cells work just like any other cell, you can use control cells to add interactivity to any Python cell. In the example below, the parameters of a plot are adjustable using control cells:

An example of adding interactivity to a Python cell

HTML outputs

If a Python cell returns an object with an HTML representation, a View output button will appear. Clicking this button will cause the output to be displayed in the output section of the cell.

The View output button appears if the object returned by a Python cell has an HTML representation.

Only one HTML output can be displayed at once - if another output is shown, the previous output will be hidden. To close an output, click the Close rich output button from the floating cell controls:

Close the output by clicking the Close rich output button.

Technical details

An object is considered to have HTML output if:

  • It has a method called _ipython_display_ which returns a string

  • It has a method called _repr_html_ which returns a string

  • It has a method called _repr_mimebundle_ which returns a dict with a key text/html

Any HTML output is contained within a sandboxed iframe, so some functionality may be restricted. Most existing packages which conform to the IPython standard methods described above should work with Count. If you encounter a package which does not work as expected, please contact Count support.

Python modules

To load a module, just import it as usual and Count will attempt to automatically download it and make it available. The first import of a new package may take a few seconds for this reason.

As your Python code is running in your browser, there are some restrictions on the modules that you can load. Available packages include:

  • pandas

  • numpy

  • matplotlib

  • scipy

  • scikit-learn

  • Any module hosted on PyPi that is written in pure Python

  • Any module specifically built for running in the browser. Many popular data-focussed modules are already supported, with more on the way. See the full list here.

Performing network requests

The popular requests and urllib3 modules are supported in Count, with the exception of streaming responses - responses are always loaded fully into memory.

When making a network request from Python, a description of the request is sent to a Count server, and the actual request is performed by an ephemeral virtual machine. The response is then proxied back through a Count server, and returned to your browser. Count does not read the response, though it may read the request to inject secrets (see below).

Maximum request payload limits apply, so network requests will fail if they attempt to send too much data.

Using secrets

If your project has any secrets configured, it is possible to use these secrets in network requests. Just format the secret name (not value!) using the secret method exposed by the count_requests module. The result of this method is just another string which looks like $$abc123$$, so it can be used in other places a string is expected. For example:

from requests import post
from count_requests import secret

req = post('<some URL>', headers={ 'Authorization': f"Bearer {secret('token')}" )

There are some security considerations to note when using secrets in Python cells:

  • Secrets can only be used in network requests.

  • Secret values are inserted into the network request once it arrives at a Count server, and then sent to the URL specified in the request.

  • Secret values are not accessible from the Count app regardless of your permission level.

  • If you grant edit access to a canvas, you should assume that the editor will be able to access any secret you have defined by, for example, sending it to a URL that they control.

  • You should never directly enter a secret value into the text of a Python cell, as it will be visible to all viewers of that canvas, even if the cell input is hidden.

Be careful when using libraries which transform secrets. For example, instead of using the auth parameter in requests (which automatically base64-encodes its arguments), construct the Authorization header manually.

Troubleshooting

See our Learn centre for frequently asked questions & how to guides on our dbt integrations.

Last updated