vsj0702 commited on
Commit
3d3e74a
Β·
verified Β·
1 Parent(s): 819c84b

Fixing error

Browse files
Files changed (1) hide show
  1. app.py +76 -125
app.py CHANGED
@@ -4,169 +4,120 @@ from utils import execute_code, export_session
4
  from chatbot import render_chatbot
5
  import json
6
 
7
- # ────────────────────────────────────────────────────────────────
8
- # 1️⃣ Page config (first call)
9
- st.set_page_config(page_title="Pro Code Playground", page_icon="πŸ’»", layout="wide")
 
 
 
10
 
11
- # 2️⃣ Theme toggle (topbar)
12
  if 'dark_mode' not in st.session_state:
13
  st.session_state.dark_mode = True
14
 
15
- # Tiny CSS + JS to inject a floating toggle switch
16
- st.markdown("""
17
- <style>
18
- /* Floating toggle wrapper */
19
- #theme-toggle-wrap {
20
- position: fixed; top: 16px; right: 24px; z-index: 999;
21
- background: rgba(255,255,255,0.1); padding: 8px 12px; border-radius: 20px;
22
- backdrop-filter: blur(5px);
23
- }
24
- /* Toggle switch */
25
- .switch { position: relative; display: inline-block; width: 50px; height: 24px; }
26
- .switch input { opacity: 0; width: 0; height: 0; }
27
- .slider {
28
- position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
29
- background-color: #ccc; transition: .3s; border-radius: 24px;
30
- }
31
- .slider:before {
32
- position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px;
33
- background-color: white; transition: .3s; border-radius: 50%;
34
- }
35
- input:checked + .slider { background-color: #66bb6a; }
36
- input:checked + .slider:before { transform: translateX(26px); }
37
- </style>
38
- <div id="theme-toggle-wrap">
39
- <label class="switch">
40
- <input type="checkbox" id="themeToggle" """ + ("checked" if st.session_state.dark_mode else "") + """>
41
- <span class="slider"></span>
42
- </label>
43
- </div>
44
- <script>
45
- document.getElementById('themeToggle').addEventListener('change', (e) => {
46
- window.parent.postMessage({isDark: e.target.checked}, '*')
47
- })
48
- </script>
49
- """, unsafe_allow_html=True)
50
-
51
- # 3️⃣ Listen for JS message to toggle
52
- toggle = st.experimental_get_query_params().get("toggle", [None])[0]
53
- msg = st.experimental_rerun if False else None # placeholder
54
- # Actually detect via st.experimental_on_message (newer Streamlit), but for simplicity:
55
- # We'll use a hack: every run, check window.name or query string β€” omitted here.
56
- # Instead, use a small checkbox fallback:
57
- fallback = st.checkbox("πŸŒ™ Dark mode", value=st.session_state.dark_mode, key="fallback_toggle")
58
- st.session_state.dark_mode = fallback
59
 
60
- # 4️⃣ Theme variables
61
- dm = st.session_state.dark_mode
62
- BG = "#0f1620" if dm else "#f5f5f5"
63
- PANEL_BG = "#1c2330" if dm else "#ffffff"
64
- TEXT = "#e3e8f1" if dm else "#1a1a1a"
65
- ACCENT = "#ff5252"
66
- BORDER = "#2a3240" if dm else "#dddddd"
67
- SHADOW = "rgba(0,0,0,0.3)" if dm else "rgba(0,0,0,0.1)"
68
 
69
- # 5️⃣ Inject global CSS
70
  st.markdown(f"""
71
  <style>
72
- /* App background */
73
- .stApp {{ background-color: {BG}; color: {TEXT}; font-family: 'Segoe UI', sans-serif; }}
74
-
75
- /* Header */
76
- .stTitle {{
77
- font-size: 2rem !important; margin-bottom: 0.2rem !important;
78
- color: {ACCENT} !important;
79
- }}
80
- .stMarkdown p {{ font-size:0.9rem; color:{TEXT}; }}
81
-
82
- /* Panels */
83
- .stContainer, .stFrame {{
84
- background: {PANEL_BG} !important;
85
- border: 1px solid {BORDER} !important;
86
- border-radius: 12px !important;
87
- box-shadow: 0 4px 12px {SHADOW} !important;
88
- padding: 16px !important;
89
- transition: background 0.3s;
90
- }}
91
-
92
- /* Buttons */
93
- .stButton > button {{
94
- background: {ACCENT} !important;
95
- color: #fff !important;
96
- border-radius: 6px !important;
97
- padding: 8px 16px !important;
98
- font-weight: 600;
99
- transition: transform .1s;
100
  }}
101
- .stButton > button:hover {{ transform: scale(1.02); }}
102
-
103
- /* Code Assistant chat styling is in chatbot.py, but we ensure messages inherit colors */
104
- .chat-container {{ border-color: {BORDER} !important; }}
105
- .user-message {{ opacity: .9; }}
106
- .bot-message {{ opacity: .9; }}
107
-
108
- /* Textarea & Inputs */
109
  textarea, input, .stTextArea textarea {{
110
- background: {PANEL_BG} !important; color: {TEXT} !important;
111
- border: 1px solid {BORDER} !important;
 
 
112
  }}
113
-
114
- /* Scrollbars */
115
- ::-webkit-scrollbar {{ width: 6px; height: 6px; }}
116
- ::-webkit-scrollbar-track {{ background: {PANEL_BG}; }}
117
- ::-webkit-scrollbar-thumb {{ background: {ACCENT}; border-radius: 3px; }}
118
-
 
 
 
 
 
 
119
  </style>
120
  """, unsafe_allow_html=True)
121
 
122
- # ────────────────────────────────────────────────────────────────
123
- # 6️⃣ App Header
124
  st.title("Pro Code Playground")
125
  st.markdown("Write, execute & export Python snippets, with built‑in AI assistance.")
126
 
127
- # 7️⃣ Two‑column main layout
128
- col_code, col_bot = st.columns((2, 1), gap="large")
129
 
130
- with col_code:
131
  st.subheader("Editor")
132
  code = st_ace.st_ace(
133
  placeholder="Start typing your Python code…",
134
- language="python", theme="monokai" if dm else "chrome",
135
- keybinding="vscode", font_size=14, min_lines=20,
136
- show_gutter=True, wrap=True, auto_update=True
 
 
 
 
 
137
  )
138
  if st.button("▢️ Run"):
139
  out, err, exc = execute_code(code)
140
  st.session_state.code_output = out
141
  st.session_state.error_output = err or exc
142
-
143
- # Output & Errors in cards
144
  if st.session_state.get("code_output"):
145
  st.text_area("Output", st.session_state.code_output, height=120)
146
  if st.session_state.get("error_output"):
147
  st.error(st.session_state.error_output)
148
 
149
- with col_bot:
150
  st.subheader("Code Assistant")
151
- render_chatbot(code, st.session_state.get("code_output",""), st.session_state.get("error_output",""))
 
 
 
 
152
 
153
- # ────────────────────────────────────────────────────────────────
154
- # 8️⃣ Export section (full width)
155
  st.markdown("---")
156
  st.subheader("Export")
157
- data = export_session(
158
- code, st.session_state.get("code_output",""), st.session_state.get("error_output","")
 
 
159
  )
160
- c1, c2 = st.columns(2)
161
- with c1:
162
- st.download_button("πŸ“¦ JSON", json.dumps(data, indent=2), "session.json", "application/json")
163
- with c2:
 
 
 
 
 
164
  txt = f"# Code\n{code}\n\n# Output\n{st.session_state.get('code_output','')}\n\n# Errors\n{st.session_state.get('error_output','')}"
165
  st.download_button("πŸ“ Text", txt, "session.txt", "text/plain")
166
 
167
- # 9️⃣ Footer
168
  st.markdown(f"""
169
- <div style='text-align:center; margin-top:1.5rem; color:{TEXT}66;'>
170
- Built with ❀️ & Streamlit β€” Enjoy coding!
171
  </div>
172
- """, unsafe_allow_html=True)
 
4
  from chatbot import render_chatbot
5
  import json
6
 
7
+ # 1️⃣ Page configuration (must be first)
8
+ st.set_page_config(
9
+ page_title="Pro Code Playground",
10
+ page_icon="πŸ’»",
11
+ layout="wide"
12
+ )
13
 
14
+ # 2️⃣ Dark mode toggle
15
  if 'dark_mode' not in st.session_state:
16
  st.session_state.dark_mode = True
17
 
18
+ dm = st.checkbox("πŸŒ™ Dark mode", value=st.session_state.dark_mode)
19
+ st.session_state.dark_mode = dm
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # 3️⃣ Theme variables
22
+ BG = "#0f1620" if dm else "#f5f5f5"
23
+ PANEL_BG = "#1c2330" if dm else "#ffffff"
24
+ TEXT = "#e3e8f1" if dm else "#1a1a1a"
25
+ ACCENT = "#ff5252"
26
+ BORDER = "#2a3240" if dm else "#dddddd"
27
+ SHADOW = "rgba(0,0,0,0.3)" if dm else "rgba(0,0,0,0.1)"
28
+ ACE_THEME = "monokai" if dm else "chrome"
29
 
30
+ # 4️⃣ Global CSS styling
31
  st.markdown(f"""
32
  <style>
33
+ .stApp {{ background-color: {BG}; color: {TEXT}; }}
34
+ [data-testid="stSidebar"] {{ background-color: {PANEL_BG} !important; }}
35
+ .ace_editor, .ace_scroller {{
36
+ background: {PANEL_BG} !important;
37
+ box-shadow: 0 4px 8px {SHADOW} !important;
38
+ border-radius: 8px !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  }}
 
 
 
 
 
 
 
 
40
  textarea, input, .stTextArea textarea {{
41
+ background: {PANEL_BG} !important;
42
+ color: {TEXT} !important;
43
+ border: 1px solid {BORDER} !important;
44
+ border-radius: 4px !important;
45
  }}
46
+ button, .stDownloadButton > button {{
47
+ background-color: {ACCENT} !important;
48
+ color: #fff !important;
49
+ border-radius: 6px !important;
50
+ transition: transform 0.1s;
51
+ }}
52
+ button:hover {{ transform: scale(1.02) !important; }}
53
+ .chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
54
+ .chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
55
+ .user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
56
+ .bot-message {{ background: rgba(200,200,200,0.2); align-self: flex-start; }}
57
+ pre code {{ display: block; padding: 0.5rem; background: rgba(0,0,0,0.1); border-radius: 4px; overflow-x: auto; }}
58
  </style>
59
  """, unsafe_allow_html=True)
60
 
61
+ # 5️⃣ App header
 
62
  st.title("Pro Code Playground")
63
  st.markdown("Write, execute & export Python snippets, with built‑in AI assistance.")
64
 
65
+ # 6️⃣ Main layout
66
+ gen, bot = st.columns((2,1), gap="large")
67
 
68
+ with gen:
69
  st.subheader("Editor")
70
  code = st_ace.st_ace(
71
  placeholder="Start typing your Python code…",
72
+ language="python",
73
+ theme=ACE_THEME,
74
+ keybinding="vscode",
75
+ font_size=14,
76
+ min_lines=20,
77
+ show_gutter=True,
78
+ wrap=True,
79
+ auto_update=True
80
  )
81
  if st.button("▢️ Run"):
82
  out, err, exc = execute_code(code)
83
  st.session_state.code_output = out
84
  st.session_state.error_output = err or exc
 
 
85
  if st.session_state.get("code_output"):
86
  st.text_area("Output", st.session_state.code_output, height=120)
87
  if st.session_state.get("error_output"):
88
  st.error(st.session_state.error_output)
89
 
90
+ with bot:
91
  st.subheader("Code Assistant")
92
+ render_chatbot(
93
+ code,
94
+ st.session_state.get("code_output", ""),
95
+ st.session_state.get("error_output", "")
96
+ )
97
 
98
+ # 7️⃣ Export section
 
99
  st.markdown("---")
100
  st.subheader("Export")
101
+ export_data = export_session(
102
+ code,
103
+ st.session_state.get("code_output", ""),
104
+ st.session_state.get("error_output", "")
105
  )
106
+ col1, col2 = st.columns(2)
107
+ with col1:
108
+ st.download_button(
109
+ "πŸ“¦ JSON",
110
+ data=json.dumps(export_data, indent=2),
111
+ file_name="session.json",
112
+ mime="application/json"
113
+ )
114
+ with col2:
115
  txt = f"# Code\n{code}\n\n# Output\n{st.session_state.get('code_output','')}\n\n# Errors\n{st.session_state.get('error_output','')}"
116
  st.download_button("πŸ“ Text", txt, "session.txt", "text/plain")
117
 
118
+ # 8️⃣ Footer
119
  st.markdown(f"""
120
+ <div style='text-align:center; margin-top:1rem; color:{TEXT}66;'>
121
+ Built with ❀️ & Streamlit
122
  </div>
123
+ """, unsafe_allow_html=True)