Jack-ki1 commited on
Commit
0f0ff4a
·
verified ·
1 Parent(s): f40c61e

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -1184
app.py DELETED
@@ -1,1184 +0,0 @@
1
- import streamlit as st
2
- import nbformat
3
- import ast
4
- import re
5
- import zipfile
6
- import io
7
- import hashlib
8
- from pathlib import Path
9
- from typing import List, Tuple, Dict, Any, Optional, Set
10
- from datetime import datetime
11
-
12
- # ==============================
13
- # SESSION STATE INITIALIZATION
14
- # ==============================
15
- def init_session_state():
16
- """Initialize all session state variables."""
17
- defaults = {
18
- 'conversion_results': {},
19
- 'uploaded_files': [],
20
- 'uploaded_zip': None,
21
- 'theme': "light",
22
- 'conversion_mode': "Hybrid (Recommended)",
23
- 'large_file_threshold': 200,
24
- 'add_main_guard': False,
25
- 'preserve_comments': True,
26
- 'conversion_stats': {
27
- 'total_files': 0,
28
- 'successful': 0,
29
- 'failed': 0,
30
- 'total_conversions': 0
31
- }
32
- }
33
- for key, value in defaults.items():
34
- if key not in st.session_state:
35
- st.session_state[key] = value
36
-
37
- init_session_state()
38
-
39
- # ==============================
40
- # PAGE CONFIG & ENHANCED THEME
41
- # ==============================
42
- st.set_page_config(
43
- page_title="📊 Python → Streamlit Converter Pro",
44
- page_icon="📊",
45
- layout="wide",
46
- initial_sidebar_state="expanded",
47
- menu_items={
48
- 'Get Help': None,
49
- 'Report a bug': None,
50
- 'About': "Python to Streamlit Converter Pro - Transform your code into beautiful Streamlit apps!"
51
- }
52
- )
53
-
54
- def apply_enhanced_theme():
55
- """Apply enhanced theme with modern styling."""
56
- is_dark = st.session_state.theme == "dark"
57
-
58
- # Color scheme
59
- if is_dark:
60
- bg_primary = "#0e1117"
61
- bg_secondary = "#1e2127"
62
- bg_tertiary = "#262730"
63
- text_primary = "#fafafa"
64
- text_secondary = "#d0d0d0"
65
- accent = "#ff4b4b"
66
- accent_hover = "#ff6b6b"
67
- border = "#3a3d47"
68
- success = "#00d4aa"
69
- warning = "#ffa726"
70
- card_bg = "#1a1d24"
71
- else:
72
- bg_primary = "#ffffff"
73
- bg_secondary = "#f8f9fa"
74
- bg_tertiary = "#e9ecef"
75
- text_primary = "#1a1a1a"
76
- text_secondary = "#4a4a4a"
77
- accent = "#ff4b4b"
78
- accent_hover = "#ff6b6b"
79
- border = "#dee2e6"
80
- success = "#00d4aa"
81
- warning = "#ffa726"
82
- card_bg = "#ffffff"
83
-
84
- st.markdown(f"""
85
- <style>
86
- /* Main App Styling */
87
- .stApp {{
88
- background: linear-gradient(135deg, {bg_primary} 0%, {bg_secondary} 100%);
89
- color: {text_primary};
90
- }}
91
-
92
- /* Typography */
93
- h1, h2, h3, h4, h5, h6 {{
94
- color: {text_primary} !important;
95
- font-weight: 700;
96
- }}
97
-
98
- /* Sidebar Styling */
99
- [data-testid="stSidebar"] {{
100
- background: {bg_secondary} !important;
101
- border-right: 1px solid {border};
102
- }}
103
-
104
- /* Cards and Containers */
105
- .main-card {{
106
- background: {card_bg};
107
- border-radius: 12px;
108
- padding: 24px;
109
- margin: 16px 0;
110
- border: 1px solid {border};
111
- box-shadow: 0 2px 8px rgba(0,0,0,0.1);
112
- }}
113
-
114
- .stat-card {{
115
- background: linear-gradient(135deg, {accent}15 0%, {accent}05 100%);
116
- border-radius: 10px;
117
- padding: 20px;
118
- border: 1px solid {accent}30;
119
- text-align: center;
120
- }}
121
-
122
- /* Buttons */
123
- .stButton > button {{
124
- border-radius: 8px;
125
- font-weight: 600;
126
- transition: all 0.3s ease;
127
- }}
128
-
129
- .stButton > button:hover {{
130
- transform: translateY(-2px);
131
- box-shadow: 0 4px 12px rgba(0,0,0,0.15);
132
- }}
133
-
134
- /* Expanders */
135
- .streamlit-expanderHeader {{
136
- background: {bg_tertiary} !important;
137
- border-radius: 8px !important;
138
- font-weight: 600;
139
- }}
140
-
141
- /* Code Blocks */
142
- .stCodeBlock {{
143
- border-radius: 8px;
144
- border: 1px solid {border};
145
- }}
146
-
147
- /* Metrics */
148
- [data-testid="stMetricValue"] {{
149
- color: {accent} !important;
150
- font-weight: 700;
151
- }}
152
-
153
- /* Tabs */
154
- .stTabs [data-baseweb="tab-list"] {{
155
- gap: 8px;
156
- }}
157
-
158
- .stTabs [data-baseweb="tab"] {{
159
- border-radius: 8px 8px 0 0;
160
- padding: 12px 24px;
161
- font-weight: 600;
162
- }}
163
-
164
- /* File Uploader */
165
- [data-testid="stFileUploader"] {{
166
- border: 2px dashed {border};
167
- border-radius: 12px;
168
- padding: 20px;
169
- background: {bg_tertiary};
170
- }}
171
-
172
- /* Progress Bar */
173
- .stProgress > div > div > div {{
174
- background: linear-gradient(90deg, {accent}, {accent_hover});
175
- }}
176
-
177
- /* Custom Badge */
178
- .custom-badge {{
179
- display: inline-block;
180
- padding: 4px 12px;
181
- border-radius: 12px;
182
- font-size: 0.85em;
183
- font-weight: 600;
184
- background: {accent}20;
185
- color: {accent};
186
- border: 1px solid {accent}40;
187
- }}
188
-
189
- /* Hero Section */
190
- .hero-section {{
191
- background: linear-gradient(135deg, {accent}15 0%, {accent}05 100%);
192
- border-radius: 16px;
193
- padding: 40px;
194
- margin: 20px 0;
195
- text-align: center;
196
- border: 1px solid {accent}30;
197
- }}
198
-
199
- /* Info Boxes */
200
- .info-box {{
201
- background: {bg_tertiary};
202
- border-left: 4px solid {accent};
203
- border-radius: 4px;
204
- padding: 16px;
205
- margin: 12px 0;
206
- }}
207
-
208
- /* Success Message */
209
- .success-box {{
210
- background: {success}15;
211
- border-left: 4px solid {success};
212
- border-radius: 4px;
213
- padding: 16px;
214
- margin: 12px 0;
215
- }}
216
- </style>
217
- """, unsafe_allow_html=True)
218
-
219
- apply_enhanced_theme()
220
-
221
- # ==============================
222
- # UI COMPONENTS
223
- # ==============================
224
-
225
- def render_stat_card(title: str, value: str, icon: str = "📊"):
226
- """Render a statistics card."""
227
- st.markdown(f"""
228
- <div class="stat-card">
229
- <div style="font-size: 2em; margin-bottom: 8px;">{icon}</div>
230
- <div style="font-size: 1.8em; font-weight: 700; color: {st.session_state.get('accent', '#ff4b4b')}; margin-bottom: 4px;">{value}</div>
231
- <div style="color: {st.session_state.get('text_secondary', '#4a4a4a')}; font-size: 0.9em;">{title}</div>
232
- </div>
233
- """, unsafe_allow_html=True)
234
-
235
- def render_badge(text: str, color: str = "#ff4b4b"):
236
- """Render a custom badge."""
237
- st.markdown(f'<span class="custom-badge" style="background: {color}20; color: {color}; border-color: {color}40;">{text}</span>', unsafe_allow_html=True)
238
-
239
- def render_hero_section():
240
- """Render the hero section."""
241
- st.markdown("""
242
- <div class="hero-section">
243
- <h1 style="font-size: 3em; margin-bottom: 16px;">🐍 Python → Streamlit Converter Pro</h1>
244
- <p style="font-size: 1.2em; color: #666; margin-bottom: 24px;">
245
- Transform your <strong>Jupyter Notebooks</strong> and <strong>Python scripts</strong> into beautiful,
246
- interactive <strong>Streamlit apps</strong> in seconds!
247
- </p>
248
- <div style="display: flex; gap: 12px; justify-content: center; flex-wrap: wrap;">
249
- <span class="custom-badge">✨ Auto-Conversion</span>
250
- <span class="custom-badge">📊 Handles Large Files</span>
251
- <span class="custom-badge">💬 Preserves Comments</span>
252
- <span class="custom-badge">📦 Batch Processing</span>
253
- </div>
254
- </div>
255
- """, unsafe_allow_html=True)
256
-
257
- # ==============================
258
- # ENHANCED SIDEBAR
259
- # ==============================
260
- with st.sidebar:
261
- # Logo and Title
262
- st.markdown("""
263
- <div style="text-align: center; padding: 20px 0;">
264
- <h1 style="font-size: 2.5em; margin: 0;">🐍</h1>
265
- <h2 style="margin-top: 8px; font-size: 1.2em;">Converter Pro</h2>
266
- </div>
267
- """, unsafe_allow_html=True)
268
-
269
- st.divider()
270
-
271
- # Quick Actions
272
- st.markdown("### ⚡ Quick Actions")
273
-
274
- col1, col2 = st.columns(2)
275
- with col1:
276
- theme_icon = "🌙" if st.session_state.theme == "light" else "☀️"
277
- if st.button(theme_icon, use_container_width=True, help="Toggle theme"):
278
- st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
279
- st.rerun()
280
-
281
- with col2:
282
- if (st.session_state.conversion_results or
283
- st.session_state.uploaded_files or
284
- st.session_state.uploaded_zip):
285
- if st.button("🔄", use_container_width=True, help="Clear all"):
286
- for key in list(st.session_state.keys()):
287
- del st.session_state[key]
288
- init_session_state()
289
- st.rerun()
290
-
291
- st.divider()
292
-
293
- # Upload Section
294
- st.markdown("### 📥 Upload Files")
295
- upload_method = st.radio(
296
- "Upload Method",
297
- ["📁 Individual Files", "📦 ZIP Archive"],
298
- index=0,
299
- help="Choose how to upload your files",
300
- label_visibility="collapsed"
301
- )
302
-
303
- if upload_method == "📁 Individual Files":
304
- uploaded_files = st.file_uploader(
305
- "Select Python or Notebook files",
306
- type=["py", "ipynb"],
307
- accept_multiple_files=True,
308
- key="file_uploader",
309
- help="Upload one or more .py or .ipynb files"
310
- )
311
- st.session_state.uploaded_files = uploaded_files if uploaded_files else []
312
- st.session_state.uploaded_zip = None
313
- else:
314
- uploaded_zip = st.file_uploader(
315
- "Upload ZIP archive",
316
- type=["zip"],
317
- key="zip_uploader",
318
- help="Upload a ZIP file containing multiple Python/Notebook files"
319
- )
320
- st.session_state.uploaded_zip = uploaded_zip
321
- st.session_state.uploaded_files = []
322
-
323
- st.divider()
324
-
325
- # Advanced Settings
326
- with st.expander("🔧 Advanced Settings", expanded=False):
327
- st.session_state.conversion_mode = st.selectbox(
328
- "🧠 Conversion Strategy",
329
- ["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"],
330
- index=["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"].index(
331
- st.session_state.conversion_mode
332
- ) if st.session_state.conversion_mode in ["Hybrid (Recommended)", "Auto", "AST (Precise)", "Regex (Fast)"] else 0,
333
- help="Hybrid combines AST and regex for best results"
334
- )
335
-
336
- st.session_state.large_file_threshold = st.slider(
337
- "📏 Large File Threshold (KB)",
338
- min_value=50,
339
- max_value=5000,
340
- value=st.session_state.large_file_threshold,
341
- help="Files larger than this use optimized processing",
342
- step=50
343
- )
344
-
345
- st.session_state.add_main_guard = st.checkbox(
346
- "🛡️ Add `if __name__ == '__main__':` guard",
347
- value=st.session_state.add_main_guard,
348
- help="Prevents execution issues when imported"
349
- )
350
-
351
- st.session_state.preserve_comments = st.checkbox(
352
- "💬 Preserve Comments & Docstrings",
353
- value=st.session_state.preserve_comments,
354
- help="Keep all comments and documentation"
355
- )
356
-
357
- st.divider()
358
-
359
- # Sample Notebook Button
360
- if st.button("🧪 Load Sample Notebook", use_container_width=True, type="secondary"):
361
- sample_nb = get_sample_notebook()
362
- sample_bytes = nbformat.writes(sample_nb).encode('utf-8')
363
- st.session_state.uploaded_files = [io.BytesIO(sample_bytes)]
364
- st.session_state.uploaded_files[0].name = "sample_notebook.ipynb"
365
- st.rerun()
366
-
367
- st.divider()
368
-
369
- # Statistics
370
- if st.session_state.conversion_stats['total_files'] > 0:
371
- st.markdown("### 📊 Statistics")
372
- stats = st.session_state.conversion_stats
373
- st.metric("Total Files", stats['total_files'])
374
- st.metric("Successful", stats['successful'], delta=f"{stats['total_conversions']} conversions")
375
- if stats['failed'] > 0:
376
- st.metric("Failed", stats['failed'], delta_color="inverse")
377
-
378
- # ==============================
379
- # SAMPLE FILE GENERATOR
380
- # ==============================
381
- @st.cache_data
382
- def get_sample_notebook():
383
- """Generate a sample notebook for testing."""
384
- nb = nbformat.v4.new_notebook()
385
- nb.cells = [
386
- nbformat.v4.new_markdown_cell("# Data Analysis Sample\n\nThis notebook demonstrates data visualization."),
387
- 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)})"),
388
- nbformat.v4.new_code_cell("print('Dataset shape:', df.shape)\nprint(f'Total rows: {len(df)}')"),
389
- nbformat.v4.new_code_cell("display(df.head())\ndisplay(df.describe())"),
390
- nbformat.v4.new_code_cell("plt.figure(figsize=(8,5))\nsns.scatterplot(data=df, x='x', y='y')\nplt.title('Scatter Plot')\nplt.show()"),
391
- nbformat.v4.new_code_cell("import plotly.express as px\nfig = px.scatter(df, x='x', y='y', title='Plotly Scatter')\nfig.show()")
392
- ]
393
- return nb
394
-
395
- # ==============================
396
- # ENHANCED CONVERTER CORE
397
- # ==============================
398
-
399
- class CommentPreservingTransformer(ast.NodeTransformer):
400
- """Enhanced AST transformer that preserves comments and handles more patterns."""
401
- def __init__(self, source_lines: List[str]):
402
- self.source_lines = source_lines
403
- self.conversion_log = []
404
- self.imports_needed = set()
405
- self.line_comments = {}
406
-
407
- def visit_Expr(self, node):
408
- """Transform expression statements like print, display, plt.show, etc."""
409
- if isinstance(node.value, ast.Call):
410
- call = node.value
411
-
412
- if self._is_print_call(call):
413
- self.conversion_log.append("Converted print() → st.write()")
414
- self.imports_needed.add("import streamlit as st")
415
- call.func = ast.Attribute(
416
- value=ast.Name(id='st', ctx=ast.Load()),
417
- attr='write',
418
- ctx=ast.Load()
419
- )
420
- return node
421
-
422
- elif self._is_display_call(call):
423
- self.conversion_log.append("Converted display() → st.dataframe()")
424
- self.imports_needed.add("import streamlit as st")
425
- call.func = ast.Attribute(
426
- value=ast.Name(id='st', ctx=ast.Load()),
427
- attr='dataframe',
428
- ctx=ast.Load()
429
- )
430
- return node
431
-
432
- elif self._is_plt_show_call(call):
433
- self.conversion_log.append("Converted plt.show() → st.pyplot()")
434
- self.imports_needed.add("import streamlit as st")
435
- self.imports_needed.add("import matplotlib.pyplot as plt")
436
- return ast.Expr(
437
- value=ast.Call(
438
- func=ast.Attribute(
439
- value=ast.Name(id='st', ctx=ast.Load()),
440
- attr='pyplot',
441
- ctx=ast.Load()
442
- ),
443
- args=[ast.Call(
444
- func=ast.Attribute(
445
- value=ast.Name(id='plt', ctx=ast.Load()),
446
- attr='gcf',
447
- ctx=ast.Load()
448
- ),
449
- args=[], keywords=[]
450
- )],
451
- keywords=[]
452
- )
453
- )
454
-
455
- elif self._is_plotly_show_call(call):
456
- var_name = self._get_call_attr_name(call)
457
- if var_name:
458
- self.conversion_log.append(f"Converted {var_name}.show() → st.plotly_chart()")
459
- self.imports_needed.add("import streamlit as st")
460
- return ast.Expr(
461
- value=ast.Call(
462
- func=ast.Attribute(
463
- value=ast.Name(id='st', ctx=ast.Load()),
464
- attr='plotly_chart',
465
- ctx=ast.Load()
466
- ),
467
- args=[ast.Name(id=var_name, ctx=ast.Load())],
468
- keywords=[]
469
- )
470
- )
471
-
472
- return self.generic_visit(node)
473
-
474
- def visit_Call(self, node):
475
- """Handle method calls like df.head(), df.tail(), etc."""
476
- if isinstance(node.func, ast.Attribute):
477
- attr_name = node.func.attr
478
-
479
- if attr_name in ('head', 'tail') and isinstance(node.func.value, (ast.Name, ast.Attribute)):
480
- parent = getattr(node, '_parent', None)
481
- if parent is None or isinstance(parent, ast.Expr):
482
- self.conversion_log.append(f"Wrapped {attr_name}() → st.dataframe()")
483
- self.imports_needed.add("import streamlit as st")
484
- return ast.Call(
485
- func=ast.Attribute(
486
- value=ast.Name(id='st', ctx=ast.Load()),
487
- attr='dataframe',
488
- ctx=ast.Load()
489
- ),
490
- args=[node],
491
- keywords=[]
492
- )
493
-
494
- return self.generic_visit(node)
495
-
496
- def _is_print_call(self, call):
497
- return (isinstance(call.func, ast.Name) and call.func.id == 'print')
498
-
499
- def _is_display_call(self, call):
500
- return (isinstance(call.func, ast.Name) and call.func.id == 'display')
501
-
502
- def _is_plt_show_call(self, call):
503
- return (isinstance(call.func, ast.Attribute) and
504
- isinstance(call.func.value, ast.Name) and
505
- call.func.value.id == 'plt' and
506
- call.func.attr == 'show')
507
-
508
- def _is_plotly_show_call(self, call):
509
- return (isinstance(call.func, ast.Attribute) and
510
- call.func.attr == 'show')
511
-
512
- def _get_call_attr_name(self, call):
513
- if isinstance(call.func, ast.Attribute):
514
- if isinstance(call.func.value, ast.Name):
515
- return call.func.value.id
516
- return None
517
-
518
-
519
- class HybridConverter:
520
- """Hybrid converter that combines AST parsing with regex for comprehensive conversion."""
521
- def __init__(self, code: str, filename: str = "unknown",
522
- conversion_mode: str = "hybrid", large_file_threshold: int = 200,
523
- add_main_guard: bool = False, preserve_comments: bool = True):
524
- self.original_code = code
525
- self.filename = filename
526
- self.conversion_mode = conversion_mode.lower()
527
- self.large_file_threshold = large_file_threshold * 1024
528
- self.add_main_guard = add_main_guard
529
- self.preserve_comments = preserve_comments
530
- self.conversion_report = []
531
- self.imports_needed = set()
532
- self.source_lines = code.splitlines(keepends=True)
533
-
534
- def convert(self) -> str:
535
- """Main conversion entry point."""
536
- file_size = len(self.original_code.encode('utf-8'))
537
- is_large = file_size > self.large_file_threshold
538
-
539
- if self.conversion_mode == "regex (fast)":
540
- self.conversion_report.append("✅ Using fast regex-based conversion")
541
- return self._regex_convert()
542
- elif self.conversion_mode == "ast (precise)":
543
- self.conversion_report.append("✅ Using precise AST-based conversion")
544
- return self._ast_convert()
545
- elif "hybrid" in self.conversion_mode:
546
- self.conversion_report.append("✅ Using hybrid conversion (AST + Regex)")
547
- return self._hybrid_convert()
548
- else: # Auto mode
549
- if is_large:
550
- self.conversion_report.append(f"✅ Large file detected ({file_size/1024:.1f}KB), using hybrid mode")
551
- return self._hybrid_convert()
552
- else:
553
- self.conversion_report.append("✅ Using AST-based conversion")
554
- return self._ast_convert()
555
-
556
- def _hybrid_convert(self) -> str:
557
- """Hybrid approach: AST for structure, regex for patterns."""
558
- try:
559
- code = self._ast_convert_core()
560
- code = self._apply_regex_patterns(code)
561
- self._detect_needed_imports(self.original_code)
562
- return self._add_streamlit_boilerplate(code)
563
- except Exception as e:
564
- self.conversion_report.append(f"⚠️ Hybrid conversion issue: {e}, falling back to regex")
565
- return self._regex_convert()
566
-
567
- def _ast_convert(self) -> str:
568
- """Pure AST-based conversion."""
569
- try:
570
- code = self._ast_convert_core()
571
- self._detect_needed_imports(self.original_code)
572
- return self._add_streamlit_boilerplate(code)
573
- except Exception as e:
574
- self.conversion_report.append(f"⚠️ AST conversion failed: {e}, falling back to regex")
575
- return self._regex_convert()
576
-
577
- def _ast_convert_core(self) -> str:
578
- """Core AST conversion logic."""
579
- try:
580
- tree = ast.parse(self.original_code, filename=self.filename)
581
- transformer = CommentPreservingTransformer(self.source_lines)
582
- transformed_tree = transformer.visit(tree)
583
- ast.fix_missing_locations(transformed_tree)
584
-
585
- self.imports_needed.update(transformer.imports_needed)
586
- self.conversion_report.extend(transformer.conversion_log)
587
-
588
- try:
589
- return ast.unparse(transformed_tree)
590
- except AttributeError:
591
- return self._ast_to_source(transformed_tree)
592
- except SyntaxError as e:
593
- self.conversion_report.append(f"⚠️ Syntax error: {e}, using line-by-line fallback")
594
- return self._line_by_line_fallback()
595
- except Exception as e:
596
- self.conversion_report.append(f"⚠️ AST parsing error: {e}")
597
- raise
598
-
599
- def _ast_to_source(self, node) -> str:
600
- """Custom AST to source converter for Python < 3.9."""
601
- try:
602
- import astor
603
- return astor.to_source(node)
604
- except ImportError:
605
- self.conversion_report.append("⚠️ Python < 3.9 detected, using regex fallback")
606
- return self._regex_convert()
607
-
608
- def _apply_regex_patterns(self, code: str) -> str:
609
- """Apply regex patterns for additional conversions."""
610
- lines = code.splitlines(keepends=True)
611
- new_lines = []
612
-
613
- for line in lines:
614
- stripped = line.strip()
615
-
616
- if stripped.startswith("%") or stripped.startswith("!"):
617
- continue
618
-
619
- if re.search(r'^\s*([a-zA-Z_]\w*\.(?:head|tail)\([^)]*\))\s*$', stripped):
620
- match = re.search(r'([a-zA-Z_]\w*\.(?:head|tail)\([^)]*\))', stripped)
621
- if match:
622
- indent = len(line) - len(line.lstrip())
623
- new_lines.append(' ' * indent + f"st.dataframe({match.group(1)})\n")
624
- self.conversion_report.append("✅ Wrapped DataFrame method → st.dataframe()")
625
- continue
626
-
627
- if re.search(r'\.show\(\)', stripped) and ('sns.' in stripped or 'seaborn' in stripped):
628
- var_match = re.search(r'([a-zA-Z_]\w*)\.show\(\)', stripped)
629
- if var_match:
630
- indent = len(line) - len(line.lstrip())
631
- new_lines.append(' ' * indent + f"st.pyplot({var_match.group(1)})\n")
632
- self.conversion_report.append("✅ Converted seaborn plot → st.pyplot()")
633
- continue
634
-
635
- new_lines.append(line)
636
-
637
- return "".join(new_lines)
638
-
639
- def _regex_convert(self) -> str:
640
- """Regex-based conversion for large files or fallback."""
641
- lines = self.source_lines
642
- new_lines = []
643
- in_multiline_string = False
644
-
645
- i = 0
646
- while i < len(lines):
647
- line = lines[i]
648
- stripped = line.strip()
649
-
650
- if stripped.startswith("%") or stripped.startswith("!"):
651
- i += 1
652
- continue
653
-
654
- if '"""' in line or "'''" in line:
655
- triple_quotes = '"""' if '"""' in line else "'''"
656
- count = line.count(triple_quotes)
657
- if count % 2 == 1:
658
- in_multiline_string = not in_multiline_string
659
- new_lines.append(line)
660
- i += 1
661
- continue
662
-
663
- if in_multiline_string:
664
- new_lines.append(line)
665
- i += 1
666
- continue
667
-
668
- if re.match(r'^\s*print\s*\(', stripped):
669
- new_line = re.sub(r'\bprint\s*\(', 'st.write(', line, count=1)
670
- new_lines.append(new_line)
671
- self.conversion_report.append("✅ Replaced print() → st.write()")
672
- self.imports_needed.add("import streamlit as st")
673
- i += 1
674
- continue
675
-
676
- if re.match(r'^\s*display\s*\(', stripped):
677
- new_line = re.sub(r'\bdisplay\s*\(', 'st.dataframe(', line, count=1)
678
- new_lines.append(new_line)
679
- self.conversion_report.append("✅ Replaced display() → st.dataframe()")
680
- self.imports_needed.add("import streamlit as st")
681
- i += 1
682
- continue
683
-
684
- if re.match(r'^\s*plt\.show\s*\(\s*\)', stripped):
685
- indent = len(line) - len(line.lstrip())
686
- new_lines.append(' ' * indent + "st.pyplot(plt.gcf())\n")
687
- self.conversion_report.append("✅ Replaced plt.show() → st.pyplot()")
688
- self.imports_needed.add("import streamlit as st")
689
- self.imports_needed.add("import matplotlib.pyplot as plt")
690
- i += 1
691
- continue
692
-
693
- if re.match(r'^\s*[a-zA-Z_]\w*\.show\s*\(\s*\)', stripped):
694
- match = re.search(r'([a-zA-Z_]\w*)\.show\s*\(\s*\)', stripped)
695
- if match:
696
- var_name = match.group(1)
697
- indent = len(line) - len(line.lstrip())
698
- new_lines.append(' ' * indent + f"st.plotly_chart({var_name})\n")
699
- self.conversion_report.append(f"✅ Replaced {var_name}.show() → st.plotly_chart()")
700
- self.imports_needed.add("import streamlit as st")
701
- i += 1
702
- continue
703
-
704
- if re.match(r'^\s*[a-zA-Z_]\w*\.(?:head|tail)\s*\([^)]*\)\s*$', stripped):
705
- match = re.search(r'([a-zA-Z_]\w*\.(?:head|tail)\s*\([^)]*\))', stripped)
706
- if match:
707
- indent = len(line) - len(line.lstrip())
708
- new_lines.append(' ' * indent + f"st.dataframe({match.group(1)})\n")
709
- self.conversion_report.append("✅ Wrapped DataFrame method → st.dataframe()")
710
- self.imports_needed.add("import streamlit as st")
711
- i += 1
712
- continue
713
-
714
- new_lines.append(line)
715
- i += 1
716
-
717
- self._detect_needed_imports(self.original_code)
718
- return self._add_streamlit_boilerplate("".join(new_lines))
719
-
720
- def _line_by_line_fallback(self) -> str:
721
- """Fallback for when AST parsing fails."""
722
- return self._regex_convert()
723
-
724
- def _detect_needed_imports(self, code: str):
725
- """Detect which imports are needed based on code content."""
726
- code_lower = code.lower()
727
- self.imports_needed.add("import streamlit as st")
728
-
729
- if 'plt.' in code or 'matplotlib' in code_lower or 'pyplot' in code_lower:
730
- self.imports_needed.add("import matplotlib.pyplot as plt")
731
- if 'sns.' in code or 'seaborn' in code_lower:
732
- self.imports_needed.add("import seaborn as sns")
733
- if 'px.' in code or 'go.' in code or 'plotly' in code_lower:
734
- self.imports_needed.add("import plotly.express as px")
735
- self.imports_needed.add("import plotly.graph_objects as go")
736
- if 'pd.' in code or 'pandas' in code_lower or 'dataframe' in code_lower:
737
- self.imports_needed.add("import pandas as pd")
738
- if 'np.' in code or 'numpy' in code_lower:
739
- self.imports_needed.add("import numpy as np")
740
-
741
- def _add_streamlit_boilerplate(self, code: str) -> str:
742
- """Add Streamlit boilerplate and imports."""
743
- imports = sorted(list(self.imports_needed))
744
-
745
- existing_imports = []
746
- code_lines = code.splitlines()
747
- for line in code_lines[:20]:
748
- if line.strip().startswith('import ') or line.strip().startswith('from '):
749
- existing_imports.append(line.strip())
750
-
751
- filtered_imports = []
752
- for imp in imports:
753
- imp_name = imp.split()[1].split('.')[0] if 'import' in imp else None
754
- if imp_name:
755
- if not any(imp_name in existing for existing in existing_imports):
756
- filtered_imports.append(imp)
757
- else:
758
- filtered_imports.append(imp)
759
-
760
- boilerplate = [
761
- "# ==============================",
762
- "# AUTO-GENERATED STREAMLIT APP",
763
- f"# Source: {self.filename}",
764
- "# Converted with Python → Streamlit Converter Pro",
765
- "# ==============================\n",
766
- *filtered_imports,
767
- "",
768
- "st.set_page_config(",
769
- " page_title='Converted App',",
770
- " layout='wide'",
771
- ")\n",
772
- "st.title('🐍 Converted Streamlit App')",
773
- f"st.caption(f'_Converted from: {self.filename}_')\n",
774
- "st.divider()\n"
775
- ]
776
-
777
- if self.add_main_guard:
778
- indented_code = "\n".join(" " + line if line.strip() else line
779
- for line in code.splitlines())
780
- return "\n".join(boilerplate) + "\nif __name__ == '__main__':\n" + indented_code
781
- else:
782
- return "\n".join(boilerplate) + code
783
-
784
- def get_conversion_report(self) -> List[str]:
785
- """Get the conversion report."""
786
- if not self.conversion_report:
787
- return ["ℹ️ No transformations applied (code may already be Streamlit-compatible)"]
788
- seen = set()
789
- unique_report = []
790
- for item in self.conversion_report:
791
- if item not in seen:
792
- seen.add(item)
793
- unique_report.append(item)
794
- return unique_report
795
-
796
-
797
- # ==============================
798
- # NOTEBOOK PROCESSING
799
- # ==============================
800
-
801
- def extract_code_from_notebook(notebook_content: str, preserve_markdown: bool = True) -> str:
802
- """Convert notebook to Python script with enhanced markdown handling."""
803
- try:
804
- nb = nbformat.reads(notebook_content, as_version=4)
805
- except Exception as e:
806
- raise ValueError(f"Invalid notebook format: {e}")
807
-
808
- lines = []
809
- cell_num = 0
810
-
811
- for cell in nb.cells:
812
- cell_num += 1
813
-
814
- if cell.cell_type == "markdown" and preserve_markdown:
815
- md_content = cell.source
816
- lines.append(f"\n# {'='*60}")
817
- lines.append(f"# MARKDOWN CELL {cell_num}")
818
- lines.append(f"# {'='*60}")
819
-
820
- for md_line in md_content.split('\n'):
821
- if not md_line.strip():
822
- lines.append("#")
823
- else:
824
- clean_line = md_line.replace('"""', "'''").replace("'''", '"""')
825
- if md_line.strip().startswith('#'):
826
- lines.append(f"# {clean_line}")
827
- else:
828
- lines.append(f"# {clean_line}")
829
- lines.append("")
830
-
831
- elif cell.cell_type == "code":
832
- code_content = cell.source
833
-
834
- if lines and lines[-1].strip():
835
- lines.append("")
836
-
837
- if hasattr(cell, 'metadata') and cell.metadata:
838
- lines.append(f"# Cell {cell_num} metadata: {cell.metadata}")
839
-
840
- lines.append(code_content)
841
-
842
- if not code_content.endswith('\n'):
843
- lines.append("")
844
-
845
- result = "\n".join(lines)
846
- if not result.endswith('\n'):
847
- result += "\n"
848
-
849
- return result
850
-
851
-
852
- # ==============================
853
- # FILE PROCESSING UTILITIES
854
- # ==============================
855
-
856
- @st.cache_data(show_spinner=False)
857
- def process_single_file_cached(file_bytes: bytes, filename: str, file_hash: str,
858
- conversion_mode: str, large_file_threshold: int,
859
- add_main_guard: bool, preserve_comments: bool):
860
- """Cached file processing function."""
861
- file_extension = Path(filename).suffix.lower()
862
-
863
- try:
864
- if file_extension == ".ipynb":
865
- original_code = extract_code_from_notebook(file_bytes.decode('utf-8'),
866
- preserve_markdown=preserve_comments)
867
- else:
868
- original_code = file_bytes.decode("utf-8")
869
-
870
- converter = HybridConverter(
871
- original_code,
872
- filename,
873
- conversion_mode=conversion_mode,
874
- large_file_threshold=large_file_threshold,
875
- add_main_guard=add_main_guard,
876
- preserve_comments=preserve_comments
877
- )
878
- streamlit_code = converter.convert()
879
-
880
- return streamlit_code, original_code, converter.get_conversion_report()
881
-
882
- except Exception as e:
883
- error_msg = f"Error processing {filename}: {str(e)}"
884
- return f"# {error_msg}\n# Original file could not be processed.", "", [f"❌ {error_msg}"]
885
-
886
- def process_single_file(uploaded_file, **kwargs):
887
- """Process a single uploaded file."""
888
- file_bytes = uploaded_file.getvalue()
889
- file_hash = hashlib.md5(file_bytes).hexdigest()[:8]
890
- return process_single_file_cached(
891
- file_bytes, uploaded_file.name, file_hash, **kwargs
892
- )
893
-
894
-
895
- # ==============================
896
- # MAIN APP UI
897
- # ==============================
898
-
899
- # Hero Section
900
- render_hero_section()
901
-
902
- # Main Tabs
903
- tab1, tab2, tab3 = st.tabs(["🚀 Convert Files", "📊 Dashboard", "ℹ️ How It Works"])
904
-
905
- with tab1:
906
- if st.session_state.uploaded_files:
907
- st.markdown(f"### 📄 Processing {len(st.session_state.uploaded_files)} File(s)")
908
-
909
- for idx, uploaded_file in enumerate(st.session_state.uploaded_files):
910
- file_key = f"file_{idx}_{uploaded_file.name}"
911
-
912
- with st.container():
913
- st.markdown(f"<div class='main-card'>", unsafe_allow_html=True)
914
-
915
- # File Header
916
- col1, col2, col3 = st.columns([3, 1, 1])
917
- with col1:
918
- st.markdown(f"#### 📑 {uploaded_file.name}")
919
- file_size = len(uploaded_file.getvalue())
920
- st.caption(f"Size: {file_size:,} bytes ({file_size/1024:.2f} KB)")
921
-
922
- with col2:
923
- file_ext = Path(uploaded_file.name).suffix
924
- render_badge(file_ext.upper().replace('.', ''), "#00d4aa")
925
-
926
- with col3:
927
- render_badge("Ready", "#ff4b4b")
928
-
929
- try:
930
- with st.spinner(f"🔄 Converting {uploaded_file.name}..."):
931
- streamlit_code, original_code, report = process_single_file(
932
- uploaded_file,
933
- conversion_mode=st.session_state.conversion_mode.lower(),
934
- large_file_threshold=st.session_state.large_file_threshold,
935
- add_main_guard=st.session_state.add_main_guard,
936
- preserve_comments=st.session_state.preserve_comments
937
- )
938
-
939
- # Update stats
940
- st.session_state.conversion_stats['total_files'] += 1
941
- st.session_state.conversion_stats['successful'] += 1
942
- st.session_state.conversion_stats['total_conversions'] += len([r for r in report if '✅' in r])
943
-
944
- st.success(f"✅ Successfully converted {uploaded_file.name}!")
945
-
946
- # View Mode Selector
947
- view_mode = st.radio(
948
- "👁️ View Mode",
949
- ["🔁 Side-by-Side", "📜 Original Only", "✨ Converted Only"],
950
- index=0,
951
- horizontal=True,
952
- key=f"view_{file_key}"
953
- )
954
-
955
- # Code Display
956
- if "Side-by-Side" in view_mode:
957
- col1, col2 = st.columns(2)
958
- with col1:
959
- st.markdown("##### 📜 Original Code")
960
- display_original = original_code[:20000] + ("..." if len(original_code) > 20000 else "")
961
- st.code(display_original, language="python", line_numbers=True)
962
- with col2:
963
- st.markdown("##### ✨ Converted Streamlit App")
964
- display_converted = streamlit_code[:20000] + ("..." if len(streamlit_code) > 20000 else "")
965
- st.code(display_converted, language="python", line_numbers=True)
966
- elif "Original Only" in view_mode:
967
- st.markdown("##### 📜 Original Code")
968
- display_original = original_code[:30000] + ("..." if len(original_code) > 30000 else "")
969
- st.code(display_original, language="python", line_numbers=True)
970
- else:
971
- st.markdown("##### ✨ Converted Streamlit App")
972
- display_converted = streamlit_code[:30000] + ("..." if len(streamlit_code) > 30000 else "")
973
- st.code(display_converted, language="python", line_numbers=True)
974
-
975
- # File Metrics
976
- col1, col2, col3, col4 = st.columns(4)
977
- with col1:
978
- st.metric("Original Size", f"{len(original_code):,}", "chars")
979
- with col2:
980
- st.metric("Converted Size", f"{len(streamlit_code):,}", "chars")
981
- with col3:
982
- size_diff = len(streamlit_code) - len(original_code)
983
- st.metric("Size Change", f"{size_diff:+,}", "chars")
984
- with col4:
985
- st.metric("Conversions", len([r for r in report if '✅' in r]), "transformations")
986
-
987
- # Download Button
988
- st.download_button(
989
- f"⬇️ Download {Path(uploaded_file.name).stem}_streamlit.py",
990
- streamlit_code,
991
- file_name=f"{Path(uploaded_file.name).stem}_streamlit.py",
992
- mime="text/plain",
993
- key=f"dl_{file_key}",
994
- use_container_width=True,
995
- type="primary"
996
- )
997
-
998
- # Conversion Report
999
- with st.expander("📋 Conversion Report", expanded=False):
1000
- for item in report:
1001
- if '✅' in item:
1002
- st.success(item)
1003
- elif '⚠️' in item:
1004
- st.warning(item)
1005
- elif '❌' in item:
1006
- st.error(item)
1007
- else:
1008
- st.info(item)
1009
-
1010
- except Exception as e:
1011
- st.session_state.conversion_stats['total_files'] += 1
1012
- st.session_state.conversion_stats['failed'] += 1
1013
- st.error(f"❌ Failed to convert `{uploaded_file.name}`: {str(e)}")
1014
- st.exception(e)
1015
-
1016
- st.markdown("</div>", unsafe_allow_html=True)
1017
- st.divider()
1018
-
1019
- elif st.session_state.uploaded_zip:
1020
- st.markdown("### 📦 Processing ZIP Archive")
1021
- try:
1022
- with st.spinner("Extracting and converting files..."):
1023
- results = {}
1024
- with zipfile.ZipFile(st.session_state.uploaded_zip) as zip_ref:
1025
- file_list = [f for f in zip_ref.namelist() if f.endswith(('.py', '.ipynb'))]
1026
- if not file_list:
1027
- st.warning("⚠️ No .py or .ipynb files found in ZIP!")
1028
- else:
1029
- progress_bar = st.progress(0)
1030
- status_text = st.empty()
1031
-
1032
- for i, filename in enumerate(file_list):
1033
- status_text.markdown(f"**Processing {i+1}/{len(file_list)}:** `{filename}`")
1034
- with zip_ref.open(filename) as f:
1035
- file_obj = io.BytesIO(f.read())
1036
- file_obj.name = filename
1037
- try:
1038
- code, orig, rep = process_single_file(
1039
- file_obj,
1040
- conversion_mode=st.session_state.conversion_mode.lower(),
1041
- large_file_threshold=st.session_state.large_file_threshold,
1042
- add_main_guard=st.session_state.add_main_guard,
1043
- preserve_comments=st.session_state.preserve_comments
1044
- )
1045
- results[filename] = (code, orig, rep)
1046
- st.session_state.conversion_stats['successful'] += 1
1047
- except Exception as e:
1048
- results[filename] = (f"# Conversion failed: {str(e)}", "", [f"❌ Error: {str(e)}"])
1049
- st.session_state.conversion_stats['failed'] += 1
1050
- progress_bar.progress((i + 1) / len(file_list))
1051
- st.session_state.conversion_stats['total_files'] += 1
1052
-
1053
- status_text.empty()
1054
- progress_bar.empty()
1055
-
1056
- # Create ZIP of results
1057
- zip_buffer = io.BytesIO()
1058
- with zipfile.ZipFile(zip_buffer, "w") as zf:
1059
- for name, (code, _, _) in results.items():
1060
- if not code.startswith("# Conversion failed"):
1061
- zf.writestr(f"{Path(name).stem}_streamlit.py", code)
1062
-
1063
- successful = len([r for r in results.values() if not r[0].startswith('# Conversion failed')])
1064
- st.success(f"✅ Successfully converted {successful}/{len(file_list)} file(s)")
1065
-
1066
- # Download Button
1067
- st.download_button(
1068
- "⬇️ Download All Converted Apps (ZIP)",
1069
- zip_buffer.getvalue(),
1070
- "streamlit_converted_apps.zip",
1071
- "application/zip",
1072
- use_container_width=True,
1073
- type="primary"
1074
- )
1075
-
1076
- # Results Display
1077
- for name, (code, orig, rep) in results.items():
1078
- with st.expander(f"📄 {name}", expanded=False):
1079
- if code.startswith("# Conversion failed"):
1080
- st.error(code)
1081
- else:
1082
- view_mode = st.radio(
1083
- "👁️ View Mode",
1084
- ["🔁 Side-by-Side", "📜 Original Only", "✨ Converted Only"],
1085
- index=0,
1086
- horizontal=True,
1087
- key=f"view_{name}"
1088
- )
1089
-
1090
- if "Side-by-Side" in view_mode:
1091
- col1, col2 = st.columns(2)
1092
- with col1:
1093
- st.markdown("##### 📜 Original")
1094
- st.code(orig[:5000] + ("..." if len(orig) > 5000 else ""), language="python")
1095
- with col2:
1096
- st.markdown("##### ✨ Converted")
1097
- st.code(code[:5000] + ("..." if len(code) > 5000 else ""), language="python")
1098
- elif "Original Only" in view_mode:
1099
- st.code(orig[:10000] + ("..." if len(orig) > 10000 else ""), language="python")
1100
- else:
1101
- st.code(code[:10000] + ("..." if len(code) > 10000 else ""), language="python")
1102
-
1103
- with st.expander("📋 Report"):
1104
- for r in rep:
1105
- if '✅' in r:
1106
- st.success(r)
1107
- elif '❌' in r:
1108
- st.error(r)
1109
- else:
1110
- st.info(r)
1111
- except Exception as e:
1112
- st.error(f"❌ ZIP processing failed: {str(e)}")
1113
- st.exception(e)
1114
- else:
1115
- st.info("""
1116
- 👈 **Get Started:**
1117
- 1. Upload files using the sidebar (or try the sample notebook)
1118
- 2. Adjust settings if needed
1119
- 3. View and download your converted Streamlit apps!
1120
- """)
1121
-
1122
- with tab2:
1123
- st.markdown("### 📊 Conversion Dashboard")
1124
-
1125
- stats = st.session_state.conversion_stats
1126
-
1127
- if stats['total_files'] > 0:
1128
- col1, col2, col3, col4 = st.columns(4)
1129
- with col1:
1130
- render_stat_card("Total Files", str(stats['total_files']), "📁")
1131
- with col2:
1132
- render_stat_card("Successful", str(stats['successful']), "✅")
1133
- with col3:
1134
- render_stat_card("Failed", str(stats['failed']), "❌")
1135
- with col4:
1136
- render_stat_card("Total Conversions", str(stats['total_conversions']), "🔄")
1137
-
1138
- # Success Rate
1139
- if stats['total_files'] > 0:
1140
- success_rate = (stats['successful'] / stats['total_files']) * 100
1141
- st.metric("Success Rate", f"{success_rate:.1f}%")
1142
- else:
1143
- st.info("📊 No conversions yet. Upload files to see statistics here!")
1144
-
1145
- with tab3:
1146
- st.markdown("### 📖 How It Works")
1147
-
1148
- st.markdown("""
1149
- <div class="info-box">
1150
- <h4>✨ Enhanced Conversion Engine</h4>
1151
- <p><strong>Hybrid Mode (Recommended)</strong>: Combines AST parsing for structure with regex for patterns.
1152
- - Preserves code structure and comments
1153
- - Handles large files efficiently
1154
- - Best balance of accuracy and performance</p>
1155
- </div>
1156
- """, unsafe_allow_html=True)
1157
-
1158
- st.markdown("""
1159
- ### 🔄 Conversion Table
1160
-
1161
- | Original Code | → Streamlit Equivalent |
1162
- |--------------|------------------------|
1163
- | `print(x)` | `st.write(x)` |
1164
- | `display(df)` | `st.dataframe(df)` |
1165
- | `df.head()` / `df.tail()` | `st.dataframe(df.head())` |
1166
- | `plt.show()` | `st.pyplot(plt.gcf())` |
1167
- | `fig.show()` (Plotly) | `st.plotly_chart(fig)` |
1168
- | Markdown cells | Commented markdown |
1169
- | All comments | Preserved |
1170
- """)
1171
-
1172
- st.markdown("""
1173
- ### 📦 Key Features
1174
-
1175
- - ✅ **Large File Support**: Handles files up to 5MB+ efficiently
1176
- - ✅ **Markdown Preservation**: Notebook markdown cells converted to comments
1177
- - ✅ **Comment Preservation**: All comments and docstrings maintained
1178
- - ✅ **ZIP Support**: Batch convert entire folders
1179
- - ✅ **Error Recovery**: Graceful fallbacks for malformed code
1180
- - ✅ **Import Detection**: Automatically adds required imports
1181
- - ✅ **Real-time Statistics**: Track your conversion progress
1182
- """)
1183
-
1184
- st.info("💡 **Pro Tip**: Use Hybrid mode for best results. It combines the accuracy of AST with the speed of regex!")