Documentation

    Python

    Write Python that reads each input as input_1, input_2 and so on, and assigns each output to output_1, output_2 and so on. Each is a Polars DataFrame. Numbered names in your code create the matching ports. Code runs in your browser, or in a hosted run in your region, and imported packages appear in the package table.

    Calculate a total in Python

    Manual Input has four rows with quantity and unit_price. Python uses Polars to add total and keeps the input columns.

    The live example needs a desktop screen. The result it produces is shown here, and the explanation and settings below work on any screen.

    Result
    quantityunit_pricetotal
    2510
    3412
    080
    6
    1. Open the Python code and find the expression multiplying quantity by unit_price. The total column contains 10, 12, 0 and NULL.
    2. Change alias("total") to alias("line_total"). Click outside the code editor, or select another block. The result column is renamed without changing its values.
    3. Add output_2 = input_1 on a new line. A second output port appears. Select it to inspect the original quantity and unit_price without the calculated column.

    When to use

    Use Python when no block does what you need, or when you want a Python package. Your code receives input tables as Polars DataFrames.

    Read the output

    The Python example imports Polars, reads input_1 and assigns the result to output_1:

    import polars as pl
    output_1 = input_1.with_columns((pl.col("quantity") * pl.col("unit_price")).alias("total"))

    The output keeps quantity and unit_price and adds total. Two items at 5 produce 10; three at 4 produce 12. A zero quantity produces 0, and the missing quantity produces NULL.

    Inputs and outputs follow the code

    Writing input_1 creates an input port with that name. Connect the upstream table to it. Writing output_1 creates the matching output port. Assign a table to it to pass that result downstream.

    Use input_2 for a second incoming table or output_2 for a second result. Ports follow numbered names found in the active code, including names in comments or strings. Removing a name removes its port, so check connections after editing code.

    Variable previews

    Hover a variable in the editor to inspect its type, dimensions or length, and a small sample from the current preview run. Dimensions describe the full variable in that run, which may use sampled input data.

    Values are recorded after each top-level statement, so a variable shows the value it had at that line. Loops and other compound statements show their final state at their last line. Values inside functions and single loop iterations aren't traced. Changing the code or input clears old previews until new results arrive.

    Errors and execution

    Code errors and missing output assignments make the block fail. Fix the code and run it again. Each run gets the input tables connected at that moment. In the browser, use the built-in http_get/http_post helpers for network requests.

    Custom code runs in your browser or in an isolated hosted run.

    For a calculation that fits a SQL expression, use Formula.

    For a simple condition that separates rows, use Filter.

    To write the step in R, use R.

    Inputs and outputs

    Ports are determined by the script. These are the ports used by the default code.

    Inputs

    1 port
    input_1Required

    Accepts: Tables.

    Accepts one connection on this port.

    Outputs

    1 port
    output_1

    Configure the block

    Python

    Read input_N as Polars DataFrames and assign output_N. NumPy, SciPy, Numba, Pandas, GeoPandas, Polars and PyArrow are bundled. Imports automatically populate the package table. Edit version constraints there; installation and compilation use your usage.

    Default: # input_1: polars DataFrame output_1 = input_1

    Store dependencies in Cloud

    Cache additional dependencies or compiled WebAssembly in your personal Cloud. Bundled dependencies need no storage. Cloud storage limits apply. Without caching, each run reinstalls or recompiles the same locked artifacts. Change package constraints to resolve a new lock. Delete unused cached artifacts in Cloud.

    Default: false

    Python FAQs

    When should I use Python instead of a built-in block?
    Use Python for custom algorithms or library operations that the existing blocks don't cover. Formula could do this too. The example is kept small to show how code blocks work.
    Are Python inputs pandas DataFrames?
    No. Inputs are Polars DataFrames, and outputs must also be Polars DataFrames. The example uses with_columns to keep existing columns and add a result.
    What happens if I don't assign an output?
    Assign a result table to every output variable in the active code. An unassigned output causes an error. Printing a table doesn't assign an output.
    Can I change the number of rows?
    Yes. Your code decides the output's rows and columns. This example keeps all four rows, but a script can filter, aggregate or reshape them.

    Prepare your data. Build it in Strata.

    Strata opens in your browser, with no account.