streamlit-execute


Namestreamlit-execute JSON
Version 0.0.9 PyPI version JSON
download
home_pageNone
SummaryStreamlit component that allows you to execute Python code using Pyodide.
upload_time2024-06-13 08:27:42
maintainerNone
docs_urlNone
authorAnas Bouzid
requires_python>=3.7
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            >[!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

```sh
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. 

```python
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.

```python
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.

```python
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.

```python
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)
```



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "streamlit-execute",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "Anas Bouzid",
    "author_email": "anasbouzid@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/87/ce/8c3c6ec815bd6ccc5e69cb99fee0f211c5e562a2a006e01eba54bf4f6db1/streamlit-execute-0.0.9.tar.gz",
    "platform": null,
    "description": ">[!WARNING]\n> This is a work in progress. This package currently only supports python code execution. The goal is to support other languages as well.\n\n# streamlit-execute\n\nA 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.\n\n## Installation instructions\n\n```sh\npip install streamlit-execute\n```\n\n## Usage instructions\n\nThere 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. \n\n```python\nimport streamlit as st\nfrom streamlit_execute import init\n\n# Execute code\nresponse = init(\"print('Hello World!')\")\nst.write(response)\n\n```\n\nIf 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.\n\n```python\nimport streamlit as st\nimport streamlit_execute as se\n\n# Create an interpreter\nresponse = se.init()\n\n# Execute code\nresponse_add = se.run(\"5 + 5\")\nst.write(response_add)\n```\n\nThe `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.\n\nHowever, 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.\n\n```python\nimport streamlit as st\nimport streamlit_execute as se\n\n# Create an interpreter\nresponse = se.init()\n\n# Connect all run calls to the interpreter\nse.connect()\n\n# Execute code\nresponse_add = se.run(\"5 + 5\")\nst.write(response_add)\n```\n\nNote 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.\n\n### Multiple Interpreters\n\nIf 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.\n\n```python\nimport streamlit as st\nimport streamlit_execute as se\n\n# Create two interpreters\nresponse_init1 = se.init(key=\"interpreter1\")\nresponse_init2 = se.init(key=\"interpreter2\")\n\n# Connect all run calls to the interpreters\nse.connect()\n\n# Run code in the first interpreter\nresponse_add1 = se.run(\"5 + 5\", target_key=\"interpreter1\")\nst.write(response_add1)\n\n# Run code in the second interpreter\nresponse_add2 = se.run(\"a=2\\na\", target_key=\"interpreter2\")\nst.write(response_add2)\n```\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Streamlit component that allows you to execute Python code using Pyodide.",
    "version": "0.0.9",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe7686456c8e7cd6374ec00ed18761c231fc5d77a57799e55f8c3f26206b3820",
                "md5": "096fe24e62b39282969360336247a875",
                "sha256": "784efde56c376b274aa4e2243a877f77998e031a58cdd25e21a45a612b773c0e"
            },
            "downloads": -1,
            "filename": "streamlit_execute-0.0.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "096fe24e62b39282969360336247a875",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5906398,
            "upload_time": "2024-06-13T08:27:40",
            "upload_time_iso_8601": "2024-06-13T08:27:40.413316Z",
            "url": "https://files.pythonhosted.org/packages/fe/76/86456c8e7cd6374ec00ed18761c231fc5d77a57799e55f8c3f26206b3820/streamlit_execute-0.0.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "87ce8c3c6ec815bd6ccc5e69cb99fee0f211c5e562a2a006e01eba54bf4f6db1",
                "md5": "0c945682a0679fd58f030555fd3d935d",
                "sha256": "ee32c6e38d0baee60df98a65bea2cbe24a959d77b553184654aa40e177c96138"
            },
            "downloads": -1,
            "filename": "streamlit-execute-0.0.9.tar.gz",
            "has_sig": false,
            "md5_digest": "0c945682a0679fd58f030555fd3d935d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 5884234,
            "upload_time": "2024-06-13T08:27:42",
            "upload_time_iso_8601": "2024-06-13T08:27:42.782482Z",
            "url": "https://files.pythonhosted.org/packages/87/ce/8c3c6ec815bd6ccc5e69cb99fee0f211c5e562a2a006e01eba54bf4f6db1/streamlit-execute-0.0.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-13 08:27:42",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "streamlit-execute"
}
        
Elapsed time: 0.94588s