Count
count.co
  • Getting Started
    • What is Count?
    • Count FAQ
    • Intro to your workspace
    • Example canvases
    • Getting started guides
      • Set up your workspace and projects
        • 1. Review workspace settings
        • 2. Create and organise your projects
      • Canvas orientation
      • Your first ad hoc analysis
        • 1. Examples and templates
        • 2. Build your first queries
        • 3. Create visuals
        • 4. Caching, local cells and scheduling
        • 5. Collaborating with a stakeholder
      • Your first report
        • 1. Examples and templates
        • 2. Filters and control cells
        • 3. Sharing your report
        • 4. Alerts
  • Connect your data
    • Database connection overview
      • Athena
      • Azure Synapse
      • BigQuery
      • Databricks
      • Microsoft SQL Server
      • MySQL
      • PostgreSQL
      • Redshift
      • Snowflake
    • Refresh database schema
    • Upload CSV files
    • dbt integration
      • ☁️dbt Cloud integration
      • 👩‍💻dbt Core integration
  • Import & Export
    • Import from other tools
      • Import Miro files
      • Import SQL files
      • Import Google Sheets
      • Import Jupyter notebooks
    • Export code and results
      • Export compiled SQL and Jinja-SQL
      • Export CSV files
      • Export images and PDF files
  • THE CANVAS
    • Navigating the canvas
      • Canvas tool bar
      • Data sidebar
      • Customizing the canvas
    • Canvas objects
      • Cells
      • Text and markdown
      • Shapes and tools
      • Sticky notes
      • Frames
      • Images
      • Embeds
      • Stamps
      • Grouping objects
      • Object order and alignment
      • Locking objects
      • Scaling objects
      • Shared styles
    • Overviews
    • Templates
    • Count AI
    • Alerts and subscriptions
      • Slack integration
    • Keyboard shortcuts
  • Querying data
    • Cells overview
      • Dynamic query compilation engine
    • SQL cells
      • Referencing other cells
      • Jinja templating
      • SQL formatting
    • Python cells
    • Visual and low-code cells
      • Calculations in visuals and low-code cells
      • Joins in visuals and low-code cells
    • Control cells
      • Single and multiple selects
      • Date controls
      • Text, number, and boolean controls
      • Custom control cells
    • Local DuckDB cells
      • DuckDB on the server
    • Query caching and scheduling
    • Manage queries and results
    • Troubleshooting
  • Visualizing data
    • Visualization overview
    • Templated visuals
    • Custom visuals
      • Marks
      • Facet
      • Subplots
      • Style
      • Filters
    • Formatting a visual
      • Axes
        • Secondary Axis
      • Colors and labels
      • Legends
      • Tooltips
    • Column summaries
    • Dynamic text
    • Every Visual Under the Sun
  • Presenting and Reporting
    • Reports and Slides
  • Count Metrics
    • Intro to Count Metrics
    • Build and edit a catalog
    • Views
      • Creating views
      • Customizing views
    • Datasets
      • Creating datasets
    • Save changes to the catalog
      • Catalog validation
      • Version control
    • Exposing catalogs to the workspace
    • Caching in Count Metrics
    • Using the catalog
      • Explore from cell
  • Sharing and Permissions
    • Real-time collaboration
    • Comments
    • Sharing permissions
    • Shared links
    • Embedding canvases
  • History and Versions
    • Version control
    • Duplicating and merging
    • Data snapshots
  • Settings and administration
    • Workspace settings
      • Workspace members
      • Groups
      • Tags
      • Billing
      • Single sign-on (SSO)
        • Okta
        • Entra ID
        • JumpCloud
        • Google
        • Generic OIDC
      • Brand
    • Connection settings
    • Project settings
    • User settings
    • Roles and permissions
  • Quick guides
    • Interactive control guides
      • Date ranges
      • Date groupings
      • Search
      • Select All
  • Resources
    • Join the Slack community
  • Blog
  • Security overview
  • Terms of use
  • Pricing FAQ
Powered by GitBook
On this page
  • Creating a Python cell
  • How Python cells work
  • Python cell outputs
  • Interactivity
  • HTML outputs
  • Python modules
  • Performing network requests
  • Troubleshooting
  1. Querying data

Python cells

PreviousSQL formattingNextVisual and low-code cells

Last updated 1 month ago

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

  • 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 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 DataFrames. Access cell results using keys or attributes on this object:

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

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:

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.

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:

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

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

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.

Troubleshooting

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

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 cells too:

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 .

If your project has any , 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:

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

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

categorical series
local DuckDB
here
auth
Learn centre
control bar
version of Python
pandas
secrets configured
An example of adding interactivity to a Python cell
The View output button appears if the object returned by a Python cell has an HTML representation.
Close the output by clicking the Close rich output button.