Skip to main content

A reusable library for safely executing Streamlit-based Python code with RestrictedPython and AST security checks

Project description

Safe Python Streamlit Executor with Streamlit library

A reusable Python library for safely executing Streamlit-based Python code with RestrictedPython and AST security checks.

Features

  • 🔒 AST-based security checking: Analyzes code before execution for dangerous patterns
  • 🚫 Import whitelisting: Only allows approved modules to be imported
  • 🛡️ RestrictedPython integration: Uses RestrictedPython for runtime protection
  • 🔧 Customizable: Easy to add or remove allowed modules
  • 📦 Reusable: Can be used in any Python application, especially Streamlit apps

Installation

bash pip install safe_streamlit_executor

Or install from source:

bash git clone https://github.com/yeungacm/safe_streamlit_executor.git 
cd safe_streamlit_executor 
pip install -e .

Quick Start

python

from safe_streamlit_executor import SafePythonExecutor
#Create executor
executor = SafePythonExecutor()
#Execute code safely
code = """
import numpy as np
arr = np.array([1, 2, 3])
print(f"Array: {arr}")
print(f"Sum: {np.sum(arr)}")
	"""
output, error = executor.execute(code)
if output:
	print("Output:", output)
if error:
	print("Error:", error)

Usage with Streamlit

python

import streamlit as st
from safe_streamlit_executor import SafePythonExecutor
executor = SafePythonExecutor()
code = st.text_area("Enter Python code:")
if st.button("Execute"):
	output, error = executor.execute(code) 
if output:
	st.code(output) 
if error:
	st.error(error)

Customization

python

Add custom allowed modules

executor.add_allowed_module('my_custom_module')

Remove modules from whitelist

executor.remove_allowed_module('os.path')

Create executor with custom module list

from safe_streamlit_executor.utils import create_executor_with_custom_modules
executor = create_executor_with_custom_modules( additional_modules={'my_module'}, remove_modules={'PIL'} )

Security Features

  • Blocks dangerous functions: eval, exec, open, compile, etc.
  • Prevents access to special attributes (__class__, __dict__, etc.)
  • Restricts imports to whitelisted modules only
  • Uses RestrictedPython for runtime protection
  • AST-based static analysis before execution

License

MIT License

Usage Example:

streamlit_app_example.py

Any Streamlit program can now use this library like this:

import streamlit as st
from safe_streamlit_executor import SafePythonExecutor

Create executor instance

executor = SafePythonExecutor()

Use in your Streamlit app

code = st.text_area("Enter code:")
if st.button("Run"):
output, error = executor.execute(code)
if output:
    st.success(output)
if error:
    st.error(error)

This library provides a clean, reusable interface for safe Python code execution that can be easily integrated into any Streamlit application or other Python project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safe_streamlit_executor-1.0.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safe_streamlit_executor-1.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file safe_streamlit_executor-1.0.1.tar.gz.

File metadata

  • Download URL: safe_streamlit_executor-1.0.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for safe_streamlit_executor-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6dd2b0b673399c6d8f3678142d27b54f6e2622e8765829cac79774d26e11bdf5
MD5 681b15e0aef3ee3fac4add26203ed70a
BLAKE2b-256 8b090d59805c2a52052e768aaf0ea3eedebdafaab45ee8e9c4eec2361fb429ad

See more details on using hashes here.

File details

Details for the file safe_streamlit_executor-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for safe_streamlit_executor-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2be1d8110377aeabd9b4dbef00a9d9a4bcf271d3359823c4d982b51f6ec37e41
MD5 190135131cf0f8b5a6b9036aa6ac089c
BLAKE2b-256 722b4849faa6800ed01ae7874e1c5bd2d32eb0abf7ea8ccdff46059fa651b44a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page