Skip to content

Python Code

The python code block can be used with its own code or code loaded from another source as a text editor with the type Python.

This block allows multiple inputs and outputs. This makes it interesting to tie the behaviors of different blocks together.

To set the inputs and outputs for the script, select "open extended view" which opens a popup.

Output

As defined in the extended view.

Input

A Python file or the user can directly write the code. Moreover in the extended view below it is possible to define input variables.

Extended view

Imagine we have the following Python script:

sum = a + b
product = a * b

You can see that a and b are not defined in this script. On the top right you can set the inputs. Enter the variable name a and accept it with the + button. You can now choose any channel from your post. Repeat this with b. Input variables can be deleted with x next to the import.

Outputs can be defined on the bottom left. Enter sum into the text field and accept ith with +. Now, choose a content type. For simple values as arrays or numbers, JSON is the best option. Like inputs, output variables can be deleted with x.

Warning

After adding the outputs please "Save Draft" in top right, "View Preview" in bottom left, and "Edit Post" in the top toolbar to continue editing. This is needed to have access to all outputs of the Python Code block. Otherwise the last output will not be available.

Parameters

Label

Add a description above the code block.

Show block

The block can be hidden from readers but its content is still available as input for other blocks. You can hide it only if you set it to execute automatically.

Execute automatically

The script will run automatically on load and any input change. If set to false, the script will have to be run explicitly.

Publicly editable

Python blocks can be set to publicly editable. This may be interesting if you want to show calculations depending on certain parameters. After a page reload the changes in the code will be lost.

Examples

Python Code Block using Pyodide on inseri.swiss.

Posts on inseri.swiss that make use of the Python code block.