| import streamlit as st
|
| import nbformat
|
| import ast
|
| import re
|
| import zipfile
|
| import io
|
| import hashlib
|
| from pathlib import Path
|
| from typing import List, Tuple, Dict, Any, Optional, Set
|
| from datetime import datetime
|
|
|
|
|
|
|
|
|
| def init_session_state():
|
| """Initialize all session state variables."""
|
| defaults = {
|
| 'conversion_results': {},
|
| 'uploaded_files': [],
|
| 'uploaded_zip': None,
|
| 'theme': "light",
|
| 'conversion_mode': "Hybrid (Recommended)",
|
| 'large_file_threshold': 200,
|
| 'add_main_guard': False,
|
| 'preserve_comments': True,
|
| 'conversion_stats': {
|
| 'total_files': 0,
|
| 'successful': 0,
|
| 'failed': 0,
|
| 'total_conversions': 0
|
| }
|
| }
|
| for key, value in defaults.items():
|
| if key not in st.session_state:
|
| st.session_state[key] = value
|
|
|
| init_session_state()
|
|
|
|
|
|
|
|
|
| st.set_page_config(
|
| page_title="π Python β Streamlit Converter Pro",
|
| page_icon="π",
|
| layout="wide",
|
| initial_sidebar_state="expanded",
|
| menu_items={
|
| 'Get Help': None,
|
| 'Report a bug': None,
|
| 'About': "Python to Streamlit Converter Pro - Transform your code into beautiful Streamlit apps!"
|
| }
|
| )
|
|
|
| def apply_enhanced_theme():
|
| """Apply enhanced theme with modern styling."""
|
| is_dark = st.session_state.theme == "dark"
|
|
|
|
|
| if is_dark:
|
| bg_primary = "#0e1117"
|
| bg_secondary = "#1e2127"
|
| bg_tertiary = "#262730"
|
| text_primary = "#fafafa"
|
| text_secondary = "#d0d0d0"
|
| accent = "#ff4b4b"
|
| accent_hover = "#ff6b6b"
|
| border = "#3a3d47"
|
| success = "#00d4aa"
|
| warning = "#ffa726"
|
| card_bg = "#1a1d24"
|
| else:
|
| bg_primary = "#ffffff"
|
| bg_secondary = "#f8f9fa"
|
| bg_tertiary = "#e9ecef"
|
| text_primary = "#1a1a1a"
|
| text_secondary = "#4a4a4a"
|
| accent = "#ff4b4b"
|
| accent_hover = "#ff6b6b"
|
| border = "#dee2e6"
|
| success = "#00d4aa"
|
| warning = "#ffa726"
|
| card_bg = "#ffffff"
|
|
|
| st.markdown(f"""
|
| <style>
|
| /* Main App Styling */
|
| .stApp {{
|
| background: linear-gradient(135deg, {bg_primary} 0%, {bg_secondary} 100%);
|
| color: {text_primary};
|
| }}
|
|
|
| /* Typography */
|
| h1, h2, h3, h4, h5, h6 {{
|
| color: {text_primary} !important;
|
| font-weight: 700;
|
| }}
|
|
|
| /* Sidebar Styling */
|
| [data-testid="stSidebar"] {{
|
| background: {bg_secondary} !important;
|
| border-right: 1px solid {border};
|
| }}
|
|
|
| /* Cards and Containers */
|
| .main-card {{
|
| background: {card_bg};
|
| border-radius: 12px;
|
| padding: 24px;
|
| margin: 16px 0;
|
| border: 1px solid {border};
|
| box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| }}
|
|
|
| .stat-card {{
|
| background: linear-gradient(135deg, {accent}15 0%, {accent}05 100%);
|
| border-radius: 10px;
|
| padding: 20px;
|
| border: 1px solid {accent}30;
|
| text-align: center;
|
| }}
|
|
|
| /* Buttons */
|
| .stButton > button {{
|
| border-radius: 8px;
|
| font-weight: 600;
|
| transition: all 0.3s ease;
|
| }}
|
|
|
| .stButton > button:hover {{
|
| transform: translateY(-2px);
|
| box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
| }}
|
|
|
| /* Expanders */
|
| .streamlit-expanderHeader {{
|
| background: {bg_tertiary} !important;
|
| border-radius: 8px !important;
|
| font-weight: 600;
|
| }}
|
|
|
| /* Code Blocks */
|
| .stCodeBlock {{
|
| border-radius: 8px;
|
| border: 1px solid {border};
|
| }}
|
|
|
| /* Metrics */
|
| [data-testid="stMetricValue"] {{
|
| color: {accent} !important;
|
| font-weight: 700;
|
| }}
|
|
|
| /* Tabs */
|
| .stTabs [data-baseweb="tab-list"] {{
|
| gap: 8px;
|
| }}
|
|
|
| .stTabs [data-baseweb="tab"] {{
|
| border-radius: 8px 8px 0 0;
|
| padding: 12px 24px;
|
| font-weight: 600;
|
| }}
|
|
|
| /* File Uploader */
|
| [data-testid="stFileUploader"] {{
|
| border: 2px dashed {border};
|
| border-radius: 12px;
|
| padding: 20px;
|
| background: {bg_tertiary};
|
| }}
|
|
|
| /* Progress Bar */
|
| .stProgress > div > div > div {{
|
| background: linear-gradient(90deg, {accent}, {accent_hover});
|
| }}
|
|
|
| /* Custom Badge */
|
| .custom-badge {{
|
| display: inline-block;
|
| padding: 4px 12px;
|
| border-radius: 12px;
|
| font-size: 0.85em;
|
| font-weight: 600;
|
| background: {accent}20;
|
| color: {accent};
|
| border: 1px solid {accent}40;
|
| }}
|
|
|
| /* Hero Section */
|
| .hero-section {{
|
| background: linear-gradient(135deg, {accent}15 0%, {accent}05 100%);
|
| border-radius: 16px;
|
| padding: 40px;
|
| margin: 20px 0;
|
| text-align: center;
|
| border: 1px solid {accent}30;
|
| }}
|
|
|
| /* Info Boxes */
|
| .info-box {{
|
| background: {bg_tertiary};
|
| border-left: 4px solid {accent};
|
| border-radius: 4px;
|
| padding: 16px;
|
| margin: 12px 0;
|
| }}
|
|
|
| /* Success Message */
|
| .success-box {{
|
| background: {success}15;
|
| border-left: 4px solid {success};
|
| border-radius: 4px;
|
| padding: 16px;
|
| margin: 12px 0;
|
| }}
|
| </style>
|
| """, unsafe_allow_html=True)
|
|
|
| apply_enhanced_theme()
|
|
|
|
|
|
|
|
|
|
|
| def render_stat_card(title: str, value: str, icon: str = "π"):
|
| """Render a statistics card."""
|
| st.markdown(f"""
|
| <div class="stat-card">
|
| <div style="font-size: 2em; margin-bottom: 8px;">{icon}</div>
|
| <div style="font-size: 1.8em; font-weight: 700; color: {st.session_state.get('accent', '#ff4b4b')}; margin-bottom: 4px;">{value}</div>
|
| <div style="color: {st.session_state.get('text_secondary', '#4a4a4a')}; font-size: 0.9em;">{title}</div>
|
| </div>
|
| """, unsafe_allow_html=True)
|
|
|
| def render_badge(text: str, color: str = "#ff4b4b"):
|
| """Render a custom badge."""
|
| st.markdown(f'<span class="custom-badge" style="background: {color}20; color: {color}; border-color: {color}40;">{text}</span>', unsafe_allow_html=True)
|
|
|
| def render_hero_section():
|
| """Render the hero section."""
|
| st.markdown("""
|
| <div class="hero-section">
|
| <h1 style="font-size: 3em; margin-bottom: 16px;">π Python β Streamlit Converter Pro</h1>
|
| <p style="font-size: 1.2em; color: #666; margin-bottom: 24px;">
|
| Transform your <strong>Jupyter Notebooks</strong> and <strong>Python scripts</strong> into beautiful,
|
| interactive <strong>Streamlit apps</strong> in seconds!
|
| </p>
|
| <div style="display: flex; gap: 12px; justify-content: center; flex-wrap: wrap;">
|
| <span class="custom-badge">β¨ Auto-Conversion</span>
|
| <span class="custom-badge">π Handles Large Files</span>
|
| <span class="custom-badge">π¬ Preserves Comments</span>
|
| <span class="custom-badge">π¦ Batch Processing</span>
|
| </div>
|
| </div>
|
| """, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
| with st.sidebar:
|
|
|
| st.markdown("""
|
| <div style="text-align: center; padding: 20px 0;">
|
| <h1 style="font-size: 2.5em; margin: 0;">π</h1>
|
| <h2 style="margin-top: 8px; font-size: 1.2em;">Converter Pro</h2>
|
| </div>
|
| """, unsafe_allow_html=True)
|
|
|
| st.divider()
|
|
|
|
|
| st.markdown("### β‘ Quick Actions")
|
|
|
| col1, col2 = st.columns(2)
|
| with col1:
|
| theme_icon = "π" if st.session_state.theme == "light" else "βοΈ"
|
| if st.button(theme_icon, use_container_width=True, help="Toggle theme"):
|
| st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
|
| st.rerun()
|
|
|
| with col2:
|
| if (st.session_state.conversion_results or
|
| st.session_state.uploaded_files or
|
| st.session_state.uploaded_zip):
|
| if st.button("π", use_container_width=True, help="Clear all"):
|
| for key in list(st.session_state.keys()):
|
| del st.session_state[key]
|
| init_session_state()
|
| st.rerun()
|
|
|
| st.divider()
|
|
|
|
|
| st.markdown("### π₯ Upload Files")
|
| upload_method = st.radio(
|
| "Upload Method",
|
| ["π Individual Files", "π¦ ZIP Archive"],
|
| index=0,
|
| help="Choose how to upload your files",
|
| label_visibility="collapsed"
|
| )
|
|
|
| if upload_method == "π Individual Files":
|
| uploaded_files = st.file_uploader(
|
| "Select Python or Notebook files",
|
| type=["py", "ipynb"],
|
| accept_multiple_files=True,
|
| key="file_uploader",
|
| help="Upload one or more .py or .ipynb files"
|
| )
|
| st.session_state.uploaded_files = uploaded_files if uploaded_files else []
|
| st.session_state.uploaded_zip = None
|
| else:
|
| uploaded_zip = st.file_uploader(
|
| "Upload ZIP archive",
|
| type=["zip"],
|
| key="zip_uploader",
|
| help="Upload a ZIP file containing multiple Python/Notebook files"
|
| )
|
| st.session_state.uploaded_zip = uploaded_zip
|
| st.session_state.uploaded_files = []
|
|
|
| st.divider()
|
|
|
|
|
| with st.expander("π§ Advanced Settings", expanded=False):
|
| st.session_state.conversion_mode = st.selectbox(
|
| "π§ Conversion Strategy",
|
| ["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"],
|
| index=["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"].index(
|
| st.session_state.conversion_mode
|
| ) if st.session_state.conversion_mode in ["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"] else 0,
|
| help="Hybrid combines AST and regex for best results"
|
| )
|
|
|
| st.session_state.large_file_threshold = st.slider(
|
| "π Large File Threshold (KB)",
|
| min_value=50,
|
| max_value=5000,
|
| value=st.session_state.large_file_threshold,
|
| help="Files larger than this use optimized processing",
|
| step=50
|
| )
|
|
|
| st.session_state.add_main_guard = st.checkbox(
|
| "π‘οΈ Add `if __name__ == '__main__':` guard",
|
| value=st.session_state.add_main_guard,
|
| help="Prevents execution issues when imported"
|
| )
|
|
|
| st.session_state.preserve_comments = st.checkbox(
|
| "π¬ Preserve Comments & Docstrings",
|
| value=st.session_state.preserve_comments,
|
| help="Keep all comments and documentation"
|
| )
|
|
|
| st.divider()
|
|
|
|
|
| if st.button("π§ͺ Load Sample Notebook", use_container_width=True, type="secondary"):
|
| sample_nb = get_sample_notebook()
|
| sample_bytes = nbformat.writes(sample_nb).encode('utf-8')
|
| st.session_state.uploaded_files = [io.BytesIO(sample_bytes)]
|
| st.session_state.uploaded_files[0].name = "sample_notebook.ipynb"
|
| st.rerun()
|
|
|
| st.divider()
|
|
|
|
|
| if st.session_state.conversion_stats['total_files'] > 0:
|
| st.markdown("### π Statistics")
|
| stats = st.session_state.conversion_stats
|
| st.metric("Total Files", stats['total_files'])
|
| st.metric("Successful", stats['successful'], delta=f"{stats['total_conversions']} conversions")
|
| if stats['failed'] > 0:
|
| st.metric("Failed", stats['failed'], delta_color="inverse")
|
|
|
|
|
|
|
|
|
| @st.cache_data
|
| def get_sample_notebook():
|
| """Generate a sample notebook for testing."""
|
| nb = nbformat.v4.new_notebook()
|
| nb.cells = [
|
| nbformat.v4.new_markdown_cell("# Data Analysis Sample\n\nThis notebook demonstrates data visualization."),
|
| nbformat.v4.new_code_cell("import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\n\n# Load sample data\ndf = pd.DataFrame({'x': np.random.randn(100), 'y': np.random.randn(100)})"),
|
| nbformat.v4.new_code_cell("print('Dataset shape:', df.shape)\nprint(f'Total rows: {len(df)}')"),
|
| nbformat.v4.new_code_cell("display(df.head())\ndisplay(df.describe())"),
|
| nbformat.v4.new_code_cell("plt.figure(figsize=(8,5))\nsns.scatterplot(data=df, x='x', y='y')\nplt.title('Scatter Plot')\nplt.show()"),
|
| nbformat.v4.new_code_cell("import plotly.express as px\nfig = px.scatter(df, x='x', y='y', title='Plotly Scatter')\nfig.show()")
|
| ]
|
| return nb
|
|
|
|
|
|
|
|
|
|
|
| class CommentPreservingTransformer(ast.NodeTransformer):
|
| """Enhanced AST transformer that preserves comments and handles more patterns."""
|
| def __init__(self, source_lines: List[str]):
|
| self.source_lines = source_lines
|
| self.conversion_log = []
|
| self.imports_needed = set()
|
| self.line_comments = {}
|
|
|
| def visit_Expr(self, node):
|
| """Transform expression statements like print, display, plt.show, etc."""
|
| if isinstance(node.value, ast.Call):
|
| call = node.value
|
|
|
| if self._is_print_call(call):
|
| self.conversion_log.append("Converted print() β st.write()")
|
| self.imports_needed.add("import streamlit as st")
|
| call.func = ast.Attribute(
|
| value=ast.Name(id='st', ctx=ast.Load()),
|
| attr='write',
|
| ctx=ast.Load()
|
| )
|
| return node
|
|
|
| elif self._is_display_call(call):
|
| self.conversion_log.append("Converted display() β st.dataframe()")
|
| self.imports_needed.add("import streamlit as st")
|
| call.func = ast.Attribute(
|
| value=ast.Name(id='st', ctx=ast.Load()),
|
| attr='dataframe',
|
| ctx=ast.Load()
|
| )
|
| return node
|
|
|
| elif self._is_plt_show_call(call):
|
| self.conversion_log.append("Converted plt.show() β st.pyplot()")
|
| self.imports_needed.add("import streamlit as st")
|
| self.imports_needed.add("import matplotlib.pyplot as plt")
|
| return ast.Expr(
|
| value=ast.Call(
|
| func=ast.Attribute(
|
| value=ast.Name(id='st', ctx=ast.Load()),
|
| attr='pyplot',
|
| ctx=ast.Load()
|
| ),
|
| args=[ast.Call(
|
| func=ast.Attribute(
|
| value=ast.Name(id='plt', ctx=ast.Load()),
|
| attr='gcf',
|
| ctx=ast.Load()
|
| ),
|
| args=[], keywords=[]
|
| )],
|
| keywords=[]
|
| )
|
| )
|
|
|
| elif self._is_plotly_show_call(call):
|
| var_name = self._get_call_attr_name(call)
|
| if var_name:
|
| self.conversion_log.append(f"Converted {var_name}.show() β st.plotly_chart()")
|
| self.imports_needed.add("import streamlit as st")
|
| return ast.Expr(
|
| value=ast.Call(
|
| func=ast.Attribute(
|
| value=ast.Name(id='st', ctx=ast.Load()),
|
| attr='plotly_chart',
|
| ctx=ast.Load()
|
| ),
|
| args=[ast.Name(id=var_name, ctx=ast.Load())],
|
| keywords=[]
|
| )
|
| )
|
|
|
| return self.generic_visit(node)
|
|
|
| def visit_Call(self, node):
|
| """Handle method calls like df.head(), df.tail(), etc."""
|
| if isinstance(node.func, ast.Attribute):
|
| attr_name = node.func.attr
|
|
|
| if attr_name in ('head', 'tail') and isinstance(node.func.value, (ast.Name, ast.Attribute)):
|
| parent = getattr(node, '_parent', None)
|
| if parent is None or isinstance(parent, ast.Expr):
|
| self.conversion_log.append(f"Wrapped {attr_name}() β st.dataframe()")
|
| self.imports_needed.add("import streamlit as st")
|
| return ast.Call(
|
| func=ast.Attribute(
|
| value=ast.Name(id='st', ctx=ast.Load()),
|
| attr='dataframe',
|
| ctx=ast.Load()
|
| ),
|
| args=[node],
|
| keywords=[]
|
| )
|
|
|
| return self.generic_visit(node)
|
|
|
| def _is_print_call(self, call):
|
| return (isinstance(call.func, ast.Name) and call.func.id == 'print')
|
|
|
| def _is_display_call(self, call):
|
| return (isinstance(call.func, ast.Name) and call.func.id == 'display')
|
|
|
| def _is_plt_show_call(self, call):
|
| return (isinstance(call.func, ast.Attribute) and
|
| isinstance(call.func.value, ast.Name) and
|
| call.func.value.id == 'plt' and
|
| call.func.attr == 'show')
|
|
|
| def _is_plotly_show_call(self, call):
|
| return (isinstance(call.func, ast.Attribute) and
|
| call.func.attr == 'show')
|
|
|
| def _get_call_attr_name(self, call):
|
| if isinstance(call.func, ast.Attribute):
|
| if isinstance(call.func.value, ast.Name):
|
| return call.func.value.id
|
| return None
|
|
|
|
|
| class HybridConverter:
|
| """Hybrid converter that combines AST parsing with regex for comprehensive conversion."""
|
| def __init__(self, code: str, filename: str = "unknown",
|
| conversion_mode: str = "hybrid", large_file_threshold: int = 200,
|
| add_main_guard: bool = False, preserve_comments: bool = True):
|
| self.original_code = code
|
| self.filename = filename
|
| self.conversion_mode = conversion_mode.lower()
|
| self.large_file_threshold = large_file_threshold * 1024
|
| self.add_main_guard = add_main_guard
|
| self.preserve_comments = preserve_comments
|
| self.conversion_report = []
|
| self.imports_needed = set()
|
| self.source_lines = code.splitlines(keepends=True)
|
|
|
| def convert(self) -> str:
|
| """Main conversion entry point."""
|
| file_size = len(self.original_code.encode('utf-8'))
|
| is_large = file_size > self.large_file_threshold
|
|
|
| if self.conversion_mode == "regex (fast)":
|
| self.conversion_report.append("β
Using fast regex-based conversion")
|
| return self._regex_convert()
|
| elif self.conversion_mode == "ast (precise)":
|
| self.conversion_report.append("β
Using precise AST-based conversion")
|
| return self._ast_convert()
|
| elif "hybrid" in self.conversion_mode:
|
| self.conversion_report.append("β
Using hybrid conversion (AST + Regex)")
|
| return self._hybrid_convert()
|
| else:
|
| if is_large:
|
| self.conversion_report.append(f"β
Large file detected ({file_size/1024:.1f}KB), using hybrid mode")
|
| return self._hybrid_convert()
|
| else:
|
| self.conversion_report.append("β
Using AST-based conversion")
|
| return self._ast_convert()
|
|
|
| def _hybrid_convert(self) -> str:
|
| """Hybrid approach: AST for structure, regex for patterns."""
|
| try:
|
| code = self._ast_convert_core()
|
| code = self._apply_regex_patterns(code)
|
| self._detect_needed_imports(self.original_code)
|
| return self._add_streamlit_boilerplate(code)
|
| except Exception as e:
|
| self.conversion_report.append(f"β οΈ Hybrid conversion issue: {e}, falling back to regex")
|
| return self._regex_convert()
|
|
|
| def _ast_convert(self) -> str:
|
| """Pure AST-based conversion."""
|
| try:
|
| code = self._ast_convert_core()
|
| self._detect_needed_imports(self.original_code)
|
| return self._add_streamlit_boilerplate(code)
|
| except Exception as e:
|
| self.conversion_report.append(f"β οΈ AST conversion failed: {e}, falling back to regex")
|
| return self._regex_convert()
|
|
|
| def _ast_convert_core(self) -> str:
|
| """Core AST conversion logic."""
|
| try:
|
| tree = ast.parse(self.original_code, filename=self.filename)
|
| transformer = CommentPreservingTransformer(self.source_lines)
|
| transformed_tree = transformer.visit(tree)
|
| ast.fix_missing_locations(transformed_tree)
|
|
|
| self.imports_needed.update(transformer.imports_needed)
|
| self.conversion_report.extend(transformer.conversion_log)
|
|
|
| try:
|
| return ast.unparse(transformed_tree)
|
| except AttributeError:
|
| return self._ast_to_source(transformed_tree)
|
| except SyntaxError as e:
|
| self.conversion_report.append(f"β οΈ Syntax error: {e}, using line-by-line fallback")
|
| return self._line_by_line_fallback()
|
| except Exception as e:
|
| self.conversion_report.append(f"β οΈ AST parsing error: {e}")
|
| raise
|
|
|
| def _ast_to_source(self, node) -> str:
|
| """Custom AST to source converter for Python < 3.9."""
|
| try:
|
| import astor
|
| return astor.to_source(node)
|
| except ImportError:
|
| self.conversion_report.append("β οΈ Python < 3.9 detected, using regex fallback")
|
| return self._regex_convert()
|
|
|
| def _apply_regex_patterns(self, code: str) -> str:
|
| """Apply regex patterns for additional conversions."""
|
| lines = code.splitlines(keepends=True)
|
| new_lines = []
|
|
|
| for line in lines:
|
| stripped = line.strip()
|
|
|
| if stripped.startswith("%") or stripped.startswith("!"):
|
| continue
|
|
|
| if re.search(r'^\s*([a-zA-Z_]\w*\.(?:head|tail)\([^)]*\))\s*$', stripped):
|
| match = re.search(r'([a-zA-Z_]\w*\.(?:head|tail)\([^)]*\))', stripped)
|
| if match:
|
| indent = len(line) - len(line.lstrip())
|
| new_lines.append(' ' * indent + f"st.dataframe({match.group(1)})\n")
|
| self.conversion_report.append("β
Wrapped DataFrame method β st.dataframe()")
|
| continue
|
|
|
| if re.search(r'\.show\(\)', stripped) and ('sns.' in stripped or 'seaborn' in stripped):
|
| var_match = re.search(r'([a-zA-Z_]\w*)\.show\(\)', stripped)
|
| if var_match:
|
| indent = len(line) - len(line.lstrip())
|
| new_lines.append(' ' * indent + f"st.pyplot({var_match.group(1)})\n")
|
| self.conversion_report.append("β
Converted seaborn plot β st.pyplot()")
|
| continue
|
|
|
| new_lines.append(line)
|
|
|
| return "".join(new_lines)
|
|
|
| def _regex_convert(self) -> str:
|
| """Regex-based conversion for large files or fallback."""
|
| lines = self.source_lines
|
| new_lines = []
|
| in_multiline_string = False
|
|
|
| i = 0
|
| while i < len(lines):
|
| line = lines[i]
|
| stripped = line.strip()
|
|
|
| if stripped.startswith("%") or stripped.startswith("!"):
|
| i += 1
|
| continue
|
|
|
| if '"""' in line or "'''" in line:
|
| triple_quotes = '"""' if '"""' in line else "'''"
|
| count = line.count(triple_quotes)
|
| if count % 2 == 1:
|
| in_multiline_string = not in_multiline_string
|
| new_lines.append(line)
|
| i += 1
|
| continue
|
|
|
| if in_multiline_string:
|
| new_lines.append(line)
|
| i += 1
|
| continue
|
|
|
| if re.match(r'^\s*print\s*\(', stripped):
|
| new_line = re.sub(r'\bprint\s*\(', 'st.write(', line, count=1)
|
| new_lines.append(new_line)
|
| self.conversion_report.append("β
Replaced print() β st.write()")
|
| self.imports_needed.add("import streamlit as st")
|
| i += 1
|
| continue
|
|
|
| if re.match(r'^\s*display\s*\(', stripped):
|
| new_line = re.sub(r'\bdisplay\s*\(', 'st.dataframe(', line, count=1)
|
| new_lines.append(new_line)
|
| self.conversion_report.append("β
Replaced display() β st.dataframe()")
|
| self.imports_needed.add("import streamlit as st")
|
| i += 1
|
| continue
|
|
|
| if re.match(r'^\s*plt\.show\s*\(\s*\)', stripped):
|
| indent = len(line) - len(line.lstrip())
|
| new_lines.append(' ' * indent + "st.pyplot(plt.gcf())\n")
|
| self.conversion_report.append("β
Replaced plt.show() β st.pyplot()")
|
| self.imports_needed.add("import streamlit as st")
|
| self.imports_needed.add("import matplotlib.pyplot as plt")
|
| i += 1
|
| continue
|
|
|
| if re.match(r'^\s*[a-zA-Z_]\w*\.show\s*\(\s*\)', stripped):
|
| match = re.search(r'([a-zA-Z_]\w*)\.show\s*\(\s*\)', stripped)
|
| if match:
|
| var_name = match.group(1)
|
| indent = len(line) - len(line.lstrip())
|
| new_lines.append(' ' * indent + f"st.plotly_chart({var_name})\n")
|
| self.conversion_report.append(f"β
Replaced {var_name}.show() β st.plotly_chart()")
|
| self.imports_needed.add("import streamlit as st")
|
| i += 1
|
| continue
|
|
|
| if re.match(r'^\s*[a-zA-Z_]\w*\.(?:head|tail)\s*\([^)]*\)\s*$', stripped):
|
| match = re.search(r'([a-zA-Z_]\w*\.(?:head|tail)\s*\([^)]*\))', stripped)
|
| if match:
|
| indent = len(line) - len(line.lstrip())
|
| new_lines.append(' ' * indent + f"st.dataframe({match.group(1)})\n")
|
| self.conversion_report.append("β
Wrapped DataFrame method β st.dataframe()")
|
| self.imports_needed.add("import streamlit as st")
|
| i += 1
|
| continue
|
|
|
| new_lines.append(line)
|
| i += 1
|
|
|
| self._detect_needed_imports(self.original_code)
|
| return self._add_streamlit_boilerplate("".join(new_lines))
|
|
|
| def _line_by_line_fallback(self) -> str:
|
| """Fallback for when AST parsing fails."""
|
| return self._regex_convert()
|
|
|
| def _detect_needed_imports(self, code: str):
|
| """Detect which imports are needed based on code content."""
|
| code_lower = code.lower()
|
| self.imports_needed.add("import streamlit as st")
|
|
|
| if 'plt.' in code or 'matplotlib' in code_lower or 'pyplot' in code_lower:
|
| self.imports_needed.add("import matplotlib.pyplot as plt")
|
| if 'sns.' in code or 'seaborn' in code_lower:
|
| self.imports_needed.add("import seaborn as sns")
|
| if 'px.' in code or 'go.' in code or 'plotly' in code_lower:
|
| self.imports_needed.add("import plotly.express as px")
|
| self.imports_needed.add("import plotly.graph_objects as go")
|
| if 'pd.' in code or 'pandas' in code_lower or 'dataframe' in code_lower:
|
| self.imports_needed.add("import pandas as pd")
|
| if 'np.' in code or 'numpy' in code_lower:
|
| self.imports_needed.add("import numpy as np")
|
|
|
| def _add_streamlit_boilerplate(self, code: str) -> str:
|
| """Add Streamlit boilerplate and imports."""
|
| imports = sorted(list(self.imports_needed))
|
|
|
| existing_imports = []
|
| code_lines = code.splitlines()
|
| for line in code_lines[:20]:
|
| if line.strip().startswith('import ') or line.strip().startswith('from '):
|
| existing_imports.append(line.strip())
|
|
|
| filtered_imports = []
|
| for imp in imports:
|
| imp_name = imp.split()[1].split('.')[0] if 'import' in imp else None
|
| if imp_name:
|
| if not any(imp_name in existing for existing in existing_imports):
|
| filtered_imports.append(imp)
|
| else:
|
| filtered_imports.append(imp)
|
|
|
| boilerplate = [
|
| "# ==============================",
|
| "# AUTO-GENERATED STREAMLIT APP",
|
| f"# Source: {self.filename}",
|
| "# Converted with Python β Streamlit Converter Pro",
|
| "# ==============================\n",
|
| *filtered_imports,
|
| "",
|
| "st.set_page_config(",
|
| " page_title='Converted App',",
|
| " layout='wide'",
|
| ")\n",
|
| "st.title('π Converted Streamlit App')",
|
| f"st.caption(f'_Converted from: {self.filename}_')\n",
|
| "st.divider()\n"
|
| ]
|
|
|
| if self.add_main_guard:
|
| indented_code = "\n".join(" " + line if line.strip() else line
|
| for line in code.splitlines())
|
| return "\n".join(boilerplate) + "\nif __name__ == '__main__':\n" + indented_code
|
| else:
|
| return "\n".join(boilerplate) + code
|
|
|
| def get_conversion_report(self) -> List[str]:
|
| """Get the conversion report."""
|
| if not self.conversion_report:
|
| return ["βΉοΈ No transformations applied (code may already be Streamlit-compatible)"]
|
| seen = set()
|
| unique_report = []
|
| for item in self.conversion_report:
|
| if item not in seen:
|
| seen.add(item)
|
| unique_report.append(item)
|
| return unique_report
|
|
|
|
|
|
|
|
|
|
|
|
|
| def extract_code_from_notebook(notebook_content: str, preserve_markdown: bool = True) -> str:
|
| """Convert notebook to Python script with enhanced markdown handling."""
|
| try:
|
| nb = nbformat.reads(notebook_content, as_version=4)
|
| except Exception as e:
|
| raise ValueError(f"Invalid notebook format: {e}")
|
|
|
| lines = []
|
| cell_num = 0
|
|
|
| for cell in nb.cells:
|
| cell_num += 1
|
|
|
| if cell.cell_type == "markdown" and preserve_markdown:
|
| md_content = cell.source
|
| lines.append(f"\n# {'='*60}")
|
| lines.append(f"# MARKDOWN CELL {cell_num}")
|
| lines.append(f"# {'='*60}")
|
|
|
| for md_line in md_content.split('\n'):
|
| if not md_line.strip():
|
| lines.append("#")
|
| else:
|
| clean_line = md_line.replace('"""', "'''").replace("'''", '"""')
|
| if md_line.strip().startswith('#'):
|
| lines.append(f"# {clean_line}")
|
| else:
|
| lines.append(f"# {clean_line}")
|
| lines.append("")
|
|
|
| elif cell.cell_type == "code":
|
| code_content = cell.source
|
|
|
| if lines and lines[-1].strip():
|
| lines.append("")
|
|
|
| if hasattr(cell, 'metadata') and cell.metadata:
|
| lines.append(f"# Cell {cell_num} metadata: {cell.metadata}")
|
|
|
| lines.append(code_content)
|
|
|
| if not code_content.endswith('\n'):
|
| lines.append("")
|
|
|
| result = "\n".join(lines)
|
| if not result.endswith('\n'):
|
| result += "\n"
|
|
|
| return result
|
|
|
|
|
|
|
|
|
|
|
|
|
| @st.cache_data(show_spinner=False)
|
| def process_single_file_cached(file_bytes: bytes, filename: str, file_hash: str,
|
| conversion_mode: str, large_file_threshold: int,
|
| add_main_guard: bool, preserve_comments: bool):
|
| """Cached file processing function."""
|
| file_extension = Path(filename).suffix.lower()
|
|
|
| try:
|
| if file_extension == ".ipynb":
|
| original_code = extract_code_from_notebook(file_bytes.decode('utf-8'),
|
| preserve_markdown=preserve_comments)
|
| else:
|
| original_code = file_bytes.decode("utf-8")
|
|
|
| converter = HybridConverter(
|
| original_code,
|
| filename,
|
| conversion_mode=conversion_mode,
|
| large_file_threshold=large_file_threshold,
|
| add_main_guard=add_main_guard,
|
| preserve_comments=preserve_comments
|
| )
|
| streamlit_code = converter.convert()
|
|
|
| return streamlit_code, original_code, converter.get_conversion_report()
|
|
|
| except Exception as e:
|
| error_msg = f"Error processing {filename}: {str(e)}"
|
| return f"# {error_msg}\n# Original file could not be processed.", "", [f"β {error_msg}"]
|
|
|
| def process_single_file(uploaded_file, **kwargs):
|
| """Process a single uploaded file."""
|
| file_bytes = uploaded_file.getvalue()
|
| file_hash = hashlib.md5(file_bytes).hexdigest()[:8]
|
| return process_single_file_cached(
|
| file_bytes, uploaded_file.name, file_hash, **kwargs
|
| )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| render_hero_section()
|
|
|
|
|
| tab1, tab2, tab3 = st.tabs(["π Convert Files", "π Dashboard", "βΉοΈ How It Works"])
|
|
|
| with tab1:
|
| if st.session_state.uploaded_files:
|
| st.markdown(f"### π Processing {len(st.session_state.uploaded_files)} File(s)")
|
|
|
| for idx, uploaded_file in enumerate(st.session_state.uploaded_files):
|
| file_key = f"file_{idx}_{uploaded_file.name}"
|
|
|
| with st.container():
|
| st.markdown(f"<div class='main-card'>", unsafe_allow_html=True)
|
|
|
|
|
| col1, col2, col3 = st.columns([3, 1, 1])
|
| with col1:
|
| st.markdown(f"#### π {uploaded_file.name}")
|
| file_size = len(uploaded_file.getvalue())
|
| st.caption(f"Size: {file_size:,} bytes ({file_size/1024:.2f} KB)")
|
|
|
| with col2:
|
| file_ext = Path(uploaded_file.name).suffix
|
| render_badge(file_ext.upper().replace('.', ''), "#00d4aa")
|
|
|
| with col3:
|
| render_badge("Ready", "#ff4b4b")
|
|
|
| try:
|
| with st.spinner(f"π Converting {uploaded_file.name}..."):
|
| streamlit_code, original_code, report = process_single_file(
|
| uploaded_file,
|
| conversion_mode=st.session_state.conversion_mode.lower(),
|
| large_file_threshold=st.session_state.large_file_threshold,
|
| add_main_guard=st.session_state.add_main_guard,
|
| preserve_comments=st.session_state.preserve_comments
|
| )
|
|
|
|
|
| st.session_state.conversion_stats['total_files'] += 1
|
| st.session_state.conversion_stats['successful'] += 1
|
| st.session_state.conversion_stats['total_conversions'] += len([r for r in report if 'β
' in r])
|
|
|
| st.success(f"β
Successfully converted {uploaded_file.name}!")
|
|
|
|
|
| view_mode = st.radio(
|
| "ποΈ View Mode",
|
| ["π Side-by-Side", "π Original Only", "β¨ Converted Only"],
|
| index=0,
|
| horizontal=True,
|
| key=f"view_{file_key}"
|
| )
|
|
|
|
|
| if "Side-by-Side" in view_mode:
|
| col1, col2 = st.columns(2)
|
| with col1:
|
| st.markdown("##### π Original Code")
|
| display_original = original_code[:20000] + ("..." if len(original_code) > 20000 else "")
|
| st.code(display_original, language="python", line_numbers=True)
|
| with col2:
|
| st.markdown("##### β¨ Converted Streamlit App")
|
| display_converted = streamlit_code[:20000] + ("..." if len(streamlit_code) > 20000 else "")
|
| st.code(display_converted, language="python", line_numbers=True)
|
| elif "Original Only" in view_mode:
|
| st.markdown("##### π Original Code")
|
| display_original = original_code[:30000] + ("..." if len(original_code) > 30000 else "")
|
| st.code(display_original, language="python", line_numbers=True)
|
| else:
|
| st.markdown("##### β¨ Converted Streamlit App")
|
| display_converted = streamlit_code[:30000] + ("..." if len(streamlit_code) > 30000 else "")
|
| st.code(display_converted, language="python", line_numbers=True)
|
|
|
|
|
| col1, col2, col3, col4 = st.columns(4)
|
| with col1:
|
| st.metric("Original Size", f"{len(original_code):,}", "chars")
|
| with col2:
|
| st.metric("Converted Size", f"{len(streamlit_code):,}", "chars")
|
| with col3:
|
| size_diff = len(streamlit_code) - len(original_code)
|
| st.metric("Size Change", f"{size_diff:+,}", "chars")
|
| with col4:
|
| st.metric("Conversions", len([r for r in report if 'β
' in r]), "transformations")
|
|
|
|
|
| st.download_button(
|
| f"β¬οΈ Download {Path(uploaded_file.name).stem}_streamlit.py",
|
| streamlit_code,
|
| file_name=f"{Path(uploaded_file.name).stem}_streamlit.py",
|
| mime="text/plain",
|
| key=f"dl_{file_key}",
|
| use_container_width=True,
|
| type="primary"
|
| )
|
|
|
|
|
| with st.expander("π Conversion Report", expanded=False):
|
| for item in report:
|
| if 'β
' in item:
|
| st.success(item)
|
| elif 'β οΈ' in item:
|
| st.warning(item)
|
| elif 'β' in item:
|
| st.error(item)
|
| else:
|
| st.info(item)
|
|
|
| except Exception as e:
|
| st.session_state.conversion_stats['total_files'] += 1
|
| st.session_state.conversion_stats['failed'] += 1
|
| st.error(f"β Failed to convert `{uploaded_file.name}`: {str(e)}")
|
| st.exception(e)
|
|
|
| st.markdown("</div>", unsafe_allow_html=True)
|
| st.divider()
|
|
|
| elif st.session_state.uploaded_zip:
|
| st.markdown("### π¦ Processing ZIP Archive")
|
| try:
|
| with st.spinner("Extracting and converting files..."):
|
| results = {}
|
| with zipfile.ZipFile(st.session_state.uploaded_zip) as zip_ref:
|
| file_list = [f for f in zip_ref.namelist() if f.endswith(('.py', '.ipynb'))]
|
| if not file_list:
|
| st.warning("β οΈ No .py or .ipynb files found in ZIP!")
|
| else:
|
| progress_bar = st.progress(0)
|
| status_text = st.empty()
|
|
|
| for i, filename in enumerate(file_list):
|
| status_text.markdown(f"**Processing {i+1}/{len(file_list)}:** `{filename}`")
|
| with zip_ref.open(filename) as f:
|
| file_obj = io.BytesIO(f.read())
|
| file_obj.name = filename
|
| try:
|
| code, orig, rep = process_single_file(
|
| file_obj,
|
| conversion_mode=st.session_state.conversion_mode.lower(),
|
| large_file_threshold=st.session_state.large_file_threshold,
|
| add_main_guard=st.session_state.add_main_guard,
|
| preserve_comments=st.session_state.preserve_comments
|
| )
|
| results[filename] = (code, orig, rep)
|
| st.session_state.conversion_stats['successful'] += 1
|
| except Exception as e:
|
| results[filename] = (f"# Conversion failed: {str(e)}", "", [f"β Error: {str(e)}"])
|
| st.session_state.conversion_stats['failed'] += 1
|
| progress_bar.progress((i + 1) / len(file_list))
|
| st.session_state.conversion_stats['total_files'] += 1
|
|
|
| status_text.empty()
|
| progress_bar.empty()
|
|
|
|
|
| zip_buffer = io.BytesIO()
|
| with zipfile.ZipFile(zip_buffer, "w") as zf:
|
| for name, (code, _, _) in results.items():
|
| if not code.startswith("# Conversion failed"):
|
| zf.writestr(f"{Path(name).stem}_streamlit.py", code)
|
|
|
| successful = len([r for r in results.values() if not r[0].startswith('# Conversion failed')])
|
| st.success(f"β
Successfully converted {successful}/{len(file_list)} file(s)")
|
|
|
|
|
| st.download_button(
|
| "β¬οΈ Download All Converted Apps (ZIP)",
|
| zip_buffer.getvalue(),
|
| "streamlit_converted_apps.zip",
|
| "application/zip",
|
| use_container_width=True,
|
| type="primary"
|
| )
|
|
|
|
|
| for name, (code, orig, rep) in results.items():
|
| with st.expander(f"π {name}", expanded=False):
|
| if code.startswith("# Conversion failed"):
|
| st.error(code)
|
| else:
|
| view_mode = st.radio(
|
| "ποΈ View Mode",
|
| ["π Side-by-Side", "π Original Only", "β¨ Converted Only"],
|
| index=0,
|
| horizontal=True,
|
| key=f"view_{name}"
|
| )
|
|
|
| if "Side-by-Side" in view_mode:
|
| col1, col2 = st.columns(2)
|
| with col1:
|
| st.markdown("##### π Original")
|
| st.code(orig[:5000] + ("..." if len(orig) > 5000 else ""), language="python")
|
| with col2:
|
| st.markdown("##### β¨ Converted")
|
| st.code(code[:5000] + ("..." if len(code) > 5000 else ""), language="python")
|
| elif "Original Only" in view_mode:
|
| st.code(orig[:10000] + ("..." if len(orig) > 10000 else ""), language="python")
|
| else:
|
| st.code(code[:10000] + ("..." if len(code) > 10000 else ""), language="python")
|
|
|
| with st.expander("π Report"):
|
| for r in rep:
|
| if 'β
' in r:
|
| st.success(r)
|
| elif 'β' in r:
|
| st.error(r)
|
| else:
|
| st.info(r)
|
| except Exception as e:
|
| st.error(f"β ZIP processing failed: {str(e)}")
|
| st.exception(e)
|
| else:
|
| st.info("""
|
| π **Get Started:**
|
| 1. Upload files using the sidebar (or try the sample notebook)
|
| 2. Adjust settings if needed
|
| 3. View and download your converted Streamlit apps!
|
| """)
|
|
|
| with tab2:
|
| st.markdown("### π Conversion Dashboard")
|
|
|
| stats = st.session_state.conversion_stats
|
|
|
| if stats['total_files'] > 0:
|
| col1, col2, col3, col4 = st.columns(4)
|
| with col1:
|
| render_stat_card("Total Files", str(stats['total_files']), "π")
|
| with col2:
|
| render_stat_card("Successful", str(stats['successful']), "β
")
|
| with col3:
|
| render_stat_card("Failed", str(stats['failed']), "β")
|
| with col4:
|
| render_stat_card("Total Conversions", str(stats['total_conversions']), "π")
|
|
|
|
|
| if stats['total_files'] > 0:
|
| success_rate = (stats['successful'] / stats['total_files']) * 100
|
| st.metric("Success Rate", f"{success_rate:.1f}%")
|
| else:
|
| st.info("π No conversions yet. Upload files to see statistics here!")
|
|
|
| with tab3:
|
| st.markdown("### π How It Works")
|
|
|
| st.markdown("""
|
| <div class="info-box">
|
| <h4>β¨ Enhanced Conversion Engine</h4>
|
| <p><strong>Hybrid Mode (Recommended)</strong>: Combines AST parsing for structure with regex for patterns.
|
| - Preserves code structure and comments
|
| - Handles large files efficiently
|
| - Best balance of accuracy and performance</p>
|
| </div>
|
| """, unsafe_allow_html=True)
|
|
|
| st.markdown("""
|
| ### π Conversion Table
|
|
|
| | Original Code | β Streamlit Equivalent |
|
| |--------------|------------------------|
|
| | `print(x)` | `st.write(x)` |
|
| | `display(df)` | `st.dataframe(df)` |
|
| | `df.head()` / `df.tail()` | `st.dataframe(df.head())` |
|
| | `plt.show()` | `st.pyplot(plt.gcf())` |
|
| | `fig.show()` (Plotly) | `st.plotly_chart(fig)` |
|
| | Markdown cells | Commented markdown |
|
| | All comments | Preserved |
|
| """)
|
|
|
| st.markdown("""
|
| ### π¦ Key Features
|
|
|
| - β
**Large File Support**: Handles files up to 5MB+ efficiently
|
| - β
**Markdown Preservation**: Notebook markdown cells converted to comments
|
| - β
**Comment Preservation**: All comments and docstrings maintained
|
| - β
**ZIP Support**: Batch convert entire folders
|
| - β
**Error Recovery**: Graceful fallbacks for malformed code
|
| - β
**Import Detection**: Automatically adds required imports
|
| - β
**Real-time Statistics**: Track your conversion progress
|
| """)
|
|
|
| st.info("π‘ **Pro Tip**: Use Hybrid mode for best results. It combines the accuracy of AST with the speed of regex!")
|
|
|