File size: 50,256 Bytes
34ac52a |
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 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 |
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
# ==============================
# SESSION STATE INITIALIZATION
# ==============================
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()
# ==============================
# PAGE CONFIG & ENHANCED THEME
# ==============================
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"
# Color scheme
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()
# ==============================
# UI COMPONENTS
# ==============================
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)
# ==============================
# ENHANCED SIDEBAR
# ==============================
with st.sidebar:
# Logo and Title
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()
# Quick Actions
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()
# Upload Section
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()
# Advanced Settings
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()
# Sample Notebook Button
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()
# Statistics
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")
# ==============================
# SAMPLE FILE GENERATOR
# ==============================
@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
# ==============================
# ENHANCED CONVERTER CORE
# ==============================
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: # Auto mode
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
# ==============================
# NOTEBOOK PROCESSING
# ==============================
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
# ==============================
# FILE PROCESSING UTILITIES
# ==============================
@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
)
# ==============================
# MAIN APP UI
# ==============================
# Hero Section
render_hero_section()
# Main Tabs
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)
# File Header
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
)
# Update stats
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 Selector
view_mode = st.radio(
"ποΈ View Mode",
["π Side-by-Side", "π Original Only", "β¨ Converted Only"],
index=0,
horizontal=True,
key=f"view_{file_key}"
)
# Code Display
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)
# File Metrics
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")
# Download Button
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"
)
# Conversion Report
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()
# Create ZIP of results
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)")
# Download Button
st.download_button(
"β¬οΈ Download All Converted Apps (ZIP)",
zip_buffer.getvalue(),
"streamlit_converted_apps.zip",
"application/zip",
use_container_width=True,
type="primary"
)
# Results Display
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']), "π")
# Success Rate
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!")
|