Update README.md
Browse files
README.md
CHANGED
|
@@ -1,212 +1,219 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
```bash
|
| 59 |
-
|
| 60 |
-
```
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
###
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
-
|
| 86 |
-
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
- **
|
| 92 |
-
- **
|
| 93 |
-
- **
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
- **
|
| 100 |
-
- **
|
| 101 |
-
- **
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
-
|
| 108 |
-
-
|
| 109 |
-
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
-
|
| 115 |
-
-
|
| 116 |
-
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
plt
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
st.
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
st.
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
###
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
##
|
| 182 |
-
|
| 183 |
-
This
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
-
|
| 192 |
-
|
| 193 |
-
##
|
| 194 |
-
|
| 195 |
-
**
|
| 196 |
-
- **
|
| 197 |
-
-
|
| 198 |
-
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
**
|
| 205 |
-
-
|
| 206 |
-
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
title: python_to_streamlit_convertor
|
| 4 |
+
sdk: streamlit
|
| 5 |
+
emoji: π
|
| 6 |
+
colorFrom: green
|
| 7 |
+
colorTo: gray
|
| 8 |
+
---
|
| 9 |
+
# Python β Streamlit Converter Pro
|
| 10 |
+
|
| 11 |
+
A powerful, production-ready tool that converts Python scripts and Jupyter Notebooks into fully functional Streamlit applications. Handles large files, preserves comments and markdown, and provides multiple conversion strategies.
|
| 12 |
+
|
| 13 |
+
## β¨ Features
|
| 14 |
+
|
| 15 |
+
### Core Capabilities
|
| 16 |
+
- **Multi-Format Support**: Convert `.py` files and `.ipynb` notebooks
|
| 17 |
+
- **Large File Handling**: Efficiently processes files up to 5MB+ with optimized algorithms
|
| 18 |
+
- **Comment Preservation**: All comments, docstrings, and inline documentation are preserved
|
| 19 |
+
- **Markdown Support**: Notebook markdown cells are converted to commented Python code
|
| 20 |
+
- **Batch Processing**: Upload ZIP archives to convert multiple files at once
|
| 21 |
+
|
| 22 |
+
### Conversion Strategies
|
| 23 |
+
|
| 24 |
+
1. **Hybrid Mode (Recommended)**: Combines AST parsing with regex patterns
|
| 25 |
+
- Best balance of accuracy and performance
|
| 26 |
+
- Handles complex code structures
|
| 27 |
+
- Preserves formatting and comments
|
| 28 |
+
|
| 29 |
+
2. **AST Mode (Precise)**: Pure abstract syntax tree transformation
|
| 30 |
+
- Deep understanding of code structure
|
| 31 |
+
- Best for complex transformations
|
| 32 |
+
- Preserves all code semantics
|
| 33 |
+
|
| 34 |
+
3. **Regex Mode (Fast)**: Pattern-based matching
|
| 35 |
+
- Fastest for very large files
|
| 36 |
+
- Good for simple conversions
|
| 37 |
+
- Efficient memory usage
|
| 38 |
+
|
| 39 |
+
4. **Auto Mode**: Automatically selects the best strategy based on file size
|
| 40 |
+
|
| 41 |
+
### What Gets Converted?
|
| 42 |
+
|
| 43 |
+
| Original Code | Streamlit Equivalent |
|
| 44 |
+
|--------------|---------------------|
|
| 45 |
+
| `print(x)` | `st.write(x)` |
|
| 46 |
+
| `display(df)` | `st.dataframe(df)` |
|
| 47 |
+
| `df.head()` / `df.tail()` | `st.dataframe(df.head())` |
|
| 48 |
+
| `plt.show()` | `st.pyplot(plt.gcf())` |
|
| 49 |
+
| `fig.show()` (Plotly) | `st.plotly_chart(fig)` |
|
| 50 |
+
| Markdown cells | Commented markdown |
|
| 51 |
+
| All comments | Preserved |
|
| 52 |
+
|
| 53 |
+
## π Installation
|
| 54 |
+
|
| 55 |
+
1. Clone or download this repository
|
| 56 |
+
2. Install dependencies:
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
pip install -r Requirements.txt
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## π Usage
|
| 63 |
+
|
| 64 |
+
### Running the Application
|
| 65 |
+
|
| 66 |
+
```bash
|
| 67 |
+
streamlit run app.py
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
The application will open in your default web browser.
|
| 71 |
+
|
| 72 |
+
### Basic Workflow
|
| 73 |
+
|
| 74 |
+
1. **Upload Files**: Use the sidebar to upload Python files or Jupyter notebooks
|
| 75 |
+
- Individual files: Upload one or more files directly
|
| 76 |
+
- ZIP archive: Upload a ZIP containing multiple files
|
| 77 |
+
|
| 78 |
+
2. **Configure Settings** (Optional):
|
| 79 |
+
- Choose conversion strategy (Hybrid recommended)
|
| 80 |
+
- Set large file threshold
|
| 81 |
+
- Enable/disable main guard
|
| 82 |
+
- Toggle comment preservation
|
| 83 |
+
|
| 84 |
+
3. **Review & Download**:
|
| 85 |
+
- View original and converted code side-by-side
|
| 86 |
+
- Check conversion report for details
|
| 87 |
+
- Download the converted Streamlit app
|
| 88 |
+
|
| 89 |
+
### Advanced Settings
|
| 90 |
+
|
| 91 |
+
- **Conversion Strategy**: Choose how the code is analyzed and converted
|
| 92 |
+
- **Large File Threshold**: Files above this size (in KB) use optimized processing
|
| 93 |
+
- **Main Guard**: Adds `if __name__ == '__main__':` wrapper for safer execution
|
| 94 |
+
- **Preserve Comments**: Keep all comments and docstrings in the output
|
| 95 |
+
|
| 96 |
+
## π― Use Cases
|
| 97 |
+
|
| 98 |
+
- **Data Science Projects**: Convert Jupyter notebooks with visualizations to interactive Streamlit dashboards
|
| 99 |
+
- **Script Migration**: Transform existing Python scripts into web applications
|
| 100 |
+
- **Batch Conversion**: Process entire project folders at once
|
| 101 |
+
- **Prototyping**: Quickly create Streamlit apps from existing code
|
| 102 |
+
|
| 103 |
+
## π§ Technical Details
|
| 104 |
+
|
| 105 |
+
### Architecture
|
| 106 |
+
|
| 107 |
+
- **AST-Based Transformation**: Uses Python's `ast` module for structural analysis
|
| 108 |
+
- **Regex Fallback**: Pattern matching for edge cases and large files
|
| 109 |
+
- **Hybrid Approach**: Combines both methods for optimal results
|
| 110 |
+
- **Error Recovery**: Graceful fallbacks when parsing fails
|
| 111 |
+
|
| 112 |
+
### Performance
|
| 113 |
+
|
| 114 |
+
- Handles files up to 5MB+ efficiently
|
| 115 |
+
- Chunked processing for large files
|
| 116 |
+
- Caching for repeated conversions
|
| 117 |
+
- Memory-efficient algorithms
|
| 118 |
+
|
| 119 |
+
### Limitations
|
| 120 |
+
|
| 121 |
+
- Does not execute code (safe conversion only)
|
| 122 |
+
- Complex interactive widgets (e.g., `ipywidgets`) require manual conversion
|
| 123 |
+
- Some edge cases in very complex code may need manual adjustment
|
| 124 |
+
- Manual review recommended before production deployment
|
| 125 |
+
|
| 126 |
+
## π Example
|
| 127 |
+
|
| 128 |
+
### Input (Jupyter Notebook Cell):
|
| 129 |
+
```python
|
| 130 |
+
import pandas as pd
|
| 131 |
+
import matplotlib.pyplot as plt
|
| 132 |
+
|
| 133 |
+
df = pd.read_csv('data.csv')
|
| 134 |
+
print(f"Dataset has {len(df)} rows")
|
| 135 |
+
display(df.head())
|
| 136 |
+
|
| 137 |
+
plt.figure(figsize=(10, 6))
|
| 138 |
+
plt.plot(df['x'], df['y'])
|
| 139 |
+
plt.show()
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
### Output (Streamlit App):
|
| 143 |
+
```python
|
| 144 |
+
import streamlit as st
|
| 145 |
+
import pandas as pd
|
| 146 |
+
import matplotlib.pyplot as plt
|
| 147 |
+
|
| 148 |
+
st.set_page_config(
|
| 149 |
+
page_title='Converted App',
|
| 150 |
+
layout='wide'
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
st.title(' Converted Streamlit App')
|
| 154 |
+
|
| 155 |
+
df = pd.read_csv('data.csv')
|
| 156 |
+
st.write(f"Dataset has {len(df)} rows")
|
| 157 |
+
st.dataframe(df.head())
|
| 158 |
+
|
| 159 |
+
plt.figure(figsize=(10, 6))
|
| 160 |
+
plt.plot(df['x'], df['y'])
|
| 161 |
+
st.pyplot(plt.gcf())
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
## π οΈ Development
|
| 165 |
+
|
| 166 |
+
### Project Structure
|
| 167 |
+
```
|
| 168 |
+
python_to_full_streamlit/
|
| 169 |
+
βββ app.py # Main Streamlit application
|
| 170 |
+
βββ Requirements.txt # Python dependencies
|
| 171 |
+
βββ README.md # This file
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
### Key Components
|
| 175 |
+
|
| 176 |
+
1. **HybridConverter**: Main conversion engine with multiple strategies
|
| 177 |
+
2. **CommentPreservingTransformer**: AST transformer that preserves code structure
|
| 178 |
+
3. **extract_code_from_notebook**: Enhanced notebook processing with markdown support
|
| 179 |
+
4. **File Processing**: Cached, efficient file handling with error recovery
|
| 180 |
+
|
| 181 |
+
## π€ Contributing
|
| 182 |
+
|
| 183 |
+
This is a production-ready converter. Improvements welcome for:
|
| 184 |
+
- Additional conversion patterns
|
| 185 |
+
- Performance optimizations
|
| 186 |
+
- Edge case handling
|
| 187 |
+
- UI/UX enhancements
|
| 188 |
+
|
| 189 |
+
## π License
|
| 190 |
+
|
| 191 |
+
This project is provided as-is for converting Python code to Streamlit applications.
|
| 192 |
+
|
| 193 |
+
## π‘ Tips
|
| 194 |
+
|
| 195 |
+
- Use **Hybrid mode** for best results on most files
|
| 196 |
+
- Enable **comment preservation** to maintain documentation
|
| 197 |
+
- For very large files (>1MB), consider using **Regex mode**
|
| 198 |
+
- Always review converted code before deployment
|
| 199 |
+
- Test the generated Streamlit app with sample data
|
| 200 |
+
|
| 201 |
+
## π Troubleshooting
|
| 202 |
+
|
| 203 |
+
**Issue**: Conversion fails on a file
|
| 204 |
+
- **Solution**: Try a different conversion mode (AST vs Regex)
|
| 205 |
+
- Check if the file has syntax errors
|
| 206 |
+
- Verify the file is valid Python/Jupyter format
|
| 207 |
+
|
| 208 |
+
**Issue**: Comments are missing
|
| 209 |
+
- **Solution**: Enable "Preserve Comments" in advanced settings
|
| 210 |
+
- Use AST or Hybrid mode instead of Regex
|
| 211 |
+
|
| 212 |
+
**Issue**: Large file processing is slow
|
| 213 |
+
- **Solution**: Increase the large file threshold
|
| 214 |
+
- Use Regex mode for very large files
|
| 215 |
+
- Process files individually instead of in ZIP
|
| 216 |
+
|
| 217 |
+
---
|
| 218 |
+
|
| 219 |
+
**Made with β€οΈ for the Streamlit community**
|