Skip to main content

๐Ÿ Python Code Execution

Overviewโ€‹

Open WebUI allows for the client-side execution of Python code in the browser, utilizing Pyodide to run scripts within a code block in a chat. This feature enables Large Language Models (LLMs) to generate Python scripts that can be executed directly in the browser, leveraging a range of libraries supported by Pyodide.

To maintain user privacy and flexibility, Open WebUI mirrors PyPI packages, avoiding direct external network requests. This approach also enables the use of Pyodide in environments without internet access.

The Open WebUI frontend includes a self-contained WASM (WebAssembly) Python environment, powered by Pyodide, which can execute basic Python scripts generated by LLMs. This environment is designed for ease of use, requiring no additional setup or installation.

Supported Librariesโ€‹

Pyodide code execution is configured to load only packages configured in scripts/prepare-pyodide.js and then added to "CodeBlock.svelte". The following Pyodide packages are currently supported in Open WebUI:

  • micropip
  • packaging
  • requests
  • beautifulsoup4
  • numpy
  • pandas
  • matplotlib
  • scikit-learn
  • scipy
  • regex

These libraries can be used to perform various tasks, such as data manipulation, machine learning, and web scraping. If the package you're wanting to run is not compiled inside of the Pyodide we ship with Open WebUIm, the package will not be able to be used.

Invoking Python Code Executionโ€‹

To execute Python code, ask an LLM within a chat to write a Python script for you. Once the LLM has generated the code, a Run button will appear at the top right-hand side of the code block. Clicking this button will execute the code using Pyodide. To display the result at the bottom of a code block, ensure there is at least a single print statement within the code to display a result.

Tips for Using Python Code Executionโ€‹

  • When writing Python code, keep in mind that the code would be running in a Pyodide environment when executed. You can inform the LLM of this by mentioning "Pyodide environment" when asking for code.
  • Research the Pyodide documentation to understand the capabilities and limitations of the environment.
  • Experiment with different libraries and scripts to explore the possibilities of Python code execution in Open WebUI.

Pyodide Documentationโ€‹

Code Exampleโ€‹

Here is an example of a simple Python script that can be executed using Pyodide:

import pandas as pd

# Create a sample DataFrame
data = {'Name': ['John', 'Anna', 'Peter'],
'Age': [28, 24, 35]}
df = pd.DataFrame(data)

# Print the DataFrame
print(df)

This script will create a sample DataFrame using pandas and print it below the code block within your chat.

Extending the List of Supported Librariesโ€‹

Want to push the boundaries of what's possible? To extend the list of supported libraries, follow these steps:

  1. Fork the Pyodide repository to create your own version.
  2. Choose new packages from the existing list of packages within Pyodide or explore high-quality packages that Open WebUI currently lacks.
  3. Integrate the new packages into your forked repository to unlock even more possibilities.