Streamlit component that allows you to execute Python code using Pyodide.
Project description
[!WARNING] This is a work in progress. This package currently only supports python code execution. The goal is to support other languages as well.
streamlit-execute
A streamlit component package that allows you to execute code on the client (which keeps the user code execution a safer distance from the server/backend). Currently, only python code execution is supported. Pyodide (Webassembly) is used for python code execution. The goal is to support other languages as well.
Installation instructions
pip install streamlit-execute
Usage instructions
There are 3 functions you will likely need to call to execute python code. If you dont care about Interpreter-like features, you can just call init
function and give it the code you want to execute.
import streamlit as st
from streamlit_execute import init
# Execute code
response = init("print('Hello World!')")
st.write(response)
If you want to execute more code in the same interpreter, you can use the run
function. This will execute the code you give it in the same interpreter that was created by the init
function.
import streamlit as st
import streamlit_execute as se
# Create an interpreter
response = se.init()
# Execute code
response_add = se.run("5 + 5")
st.write(response_add)
The run
function creates another component that will pass code to the correct interpreter and receive the corresponding result. It will then pass the result back to Streamlit in the return value of the run
function.
However, the above will not work without calling an additional function that sets up communication between the interpreter component and all of the run
components. This is done by calling the connect
function.
import streamlit as st
import streamlit_execute as se
# Create an interpreter
response = se.init()
# Connect all run calls to the interpreter
se.connect()
# Execute code
response_add = se.run("5 + 5")
st.write(response_add)
Note that run
components are intended to be more lightweight than the init
component. Using multiple components was a decision that was made because using only one component requires script reruns in order to pass new code to the interpreter. This requires more sophisticated logic to implement and is not as dev-friendly.
Multiple Interpreters
If you want to use multiple interpreters, you can do so by calling the init
function multiple times. But this time, they must have different keys. This is necessary because the run
function uses the key to determine which interpreter to pass the code to. If you don't provide a key, the default key is used for both the init
and run
functions.
import streamlit as st
import streamlit_execute as se
# Create two interpreters
response_init1 = se.init(key="interpreter1")
response_init2 = se.init(key="interpreter2")
# Connect all run calls to the interpreters
se.connect()
# Run code in the first interpreter
response_add1 = se.run("5 + 5", target_key="interpreter1")
st.write(response_add1)
# Run code in the second interpreter
response_add2 = se.run("a=2\na", target_key="interpreter2")
st.write(response_add2)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for streamlit_execute-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe59dad03987e2ab996cbc2103e816c2dc7cc854ccdcd853eee8cc1da1e93d02 |
|
MD5 | 1caa67ff7bc05635b9fb3f11b60d530f |
|
BLAKE2b-256 | e11dfaf14db8ff2b802a4fd1d33bf1130d6c868a4dae10f74292dea8dc535c6f |