Streamlit Rust Backend
A pure Rust implementation of Streamlit backend with WebSocket support, compatible with the official Streamlit frontend.
Features
- 🦀 Pure Rust - No Python dependencies, completely written in Rust
- 🚀 High Performance - Leverages Rust's performance and safety features
- 🔄 WebSocket Compatible - Compatible with official Streamlit frontend
- 🎨 Python-like API - Familiar Streamlit syntax (
st.write(),st.button(), etc.) - 🔧 Extensible - Easy to add new widgets and components
- 📦 Modern Dependencies - Uses latest versions of actix-web, tokio, and other crates
Project Structure
This is a Cargo workspace with the following members:
streamlit-rust/
├── streamlit/ # Main library crate
│ ├── src/
│ │ ├── api.rs # Streamlit API implementation
│ │ ├── server.rs # HTTP and WebSocket server
│ │ ├── elements/ # UI element implementations
│ │ ├── websocket/ # WebSocket handlers
│ │ ├── error.rs # Error types
│ │ └── lib.rs # Public API
│ └── examples/ # Example applications
└── streamlit-macros/ # Procedural macros (#[main])
Quick Start
Running Examples
The easiest way to try out streamlit-rust is to run the included examples:
# Run the basic example
# Run the hello world example
# Run the timer example
# Run the container/columns example
The server will start on http://localhost:8501 by default.
Basic Usage
use *;
async
API Endpoints
- WebSocket:
ws://localhost:8501/_stcore/stream- Main WebSocket connection - Health Check:
GET /_stcore/health- Server status - Run Script:
POST /api/run- Execute Rust code - Index:
GET /- Basic info page
Available Components
Text Display
st.write(content)- Plain text (supports markdown)st.title(content)/st.h1(content)- Title (h1)st.header(content)/st.h2(content)- Header (h2)st.sub_header(content)/st.h3(content)- Sub-header (h3)st.markdown(content)- Markdown content with extended optionsst.caption(content)- Small caption textst.badge(label)- Badge/icon component
Code Display
st.code(code, language)- Syntax-highlighted code blockCodeOptions::new(code, language)- Advanced code display options.line_numbers(bool)- Show line numbers.wrap_lines(bool)- Wrap long lines.height(...)- Set height.width(...)- Set width
Layout Components
st.container()- Container for grouping elementsst.container_options(...)- Container with options.border(bool)- Show border.horizontal(bool)- Horizontal layout.gap(...)- Set gap size.alignment(...)- Set alignment
st.columns(n)- Create N columnsst.columns([v1, v2, ...])- Create columns with custom weightsColumnsOptions::new(...)- Advanced column options
Interactive Components
st.button(label)- Button (returns true if clicked)st.button_options(...)- Button with options.key(...)- Unique key.help(...)- Help tooltip.type(...)- Button type (primary/secondary).icon(...)- Icon.disabled(bool)- Disable button.shortcut(...)- Keyboard shortcut
Utility Components
st.divider()- Horizontal dividerst.divider_options(...)- Custom divider width
Architecture
┌─────────────────┐ ┌─────────────────┐
│ Streamlit │ │ Streamlit │
│ Frontend │◄──►│ Rust Backend │
│ (JavaScript) │ │ (WebSocket) │
└─────────────────┘ └─────────────────┘
- Frontend: Official Streamlit frontend connects via WebSocket
- Backend: Rust server processes widget requests and sends responses
- Protocol: Compatible with official Streamlit WebSocket protocol
Development
Prerequisites
- Rust 2024 edition or later
- Cargo
Building
# Build the workspace
# Build with optimizations
Running Tests
Formatting
Examples
Hello World
use *;
async
Markdown and Headers
use *;
async
Code Display
use *;
async
Layout with Containers and Columns
use *;
async
Buttons and Interactions
use *;
async
Badge and Caption
use *;
async
Compatibility
This implementation is designed to be compatible with:
- ✅ Official Streamlit frontend
- ✅ WebSocket protocol
- ✅ Standard Streamlit widgets and layout components
- 🔄 More widgets (slider, text_input, checkbox, etc.) - planned
- 🔄 Session management - planned
- 🔄 Advanced features - planned
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Add your improvements
- Ensure tests pass (
cargo test) - Send a pull request
License
Apache License 2.0 - see LICENSE file for details.
Acknowledgments
- Official Streamlit team for the amazing frontend and protocol
- Rust community for excellent web framework support
- actix-web and actix-ws maintainers for the solid WebSocket implementation