Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
ยท
75ef56d
1
Parent(s):
327366d
2024 Dec 30 : improved playground
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -18,6 +18,8 @@ class StreamlitTutorial:
|
|
| 18 |
self.init_session_state()
|
| 19 |
self.setup_styles()
|
| 20 |
|
|
|
|
|
|
|
| 21 |
def init_session_state(self) -> None:
|
| 22 |
"""
|
| 23 |
Initializes session state variables for persistent data storage.
|
|
@@ -31,6 +33,8 @@ class StreamlitTutorial:
|
|
| 31 |
st.session_state.code_input = ""
|
| 32 |
st.session_state.current_topic = "Basic Text Elements"
|
| 33 |
|
|
|
|
|
|
|
| 34 |
def setup_styles(self) -> None:
|
| 35 |
"""
|
| 36 |
Configures custom CSS styles for the application.
|
|
@@ -63,6 +67,9 @@ class StreamlitTutorial:
|
|
| 63 |
|
| 64 |
# Add these helper methods in the StreamlitTutorial class
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
def _get_basic_concepts_content(self) -> str:
|
| 67 |
"""
|
| 68 |
Provides content for Basic Concepts section.
|
|
@@ -84,6 +91,11 @@ class StreamlitTutorial:
|
|
| 84 |
4. Colored text
|
| 85 |
"""
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def _get_best_practices_content(self) -> str:
|
| 88 |
"""
|
| 89 |
Provides content for Best Practices section.
|
|
@@ -108,6 +120,9 @@ class StreamlitTutorial:
|
|
| 108 |
```
|
| 109 |
"""
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
def _get_examples_content(self) -> str:
|
| 112 |
"""
|
| 113 |
Provides content for Examples section.
|
|
@@ -140,6 +155,11 @@ class StreamlitTutorial:
|
|
| 140 |
```
|
| 141 |
"""
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
def _get_common_mistakes_content(self) -> str:
|
| 144 |
"""
|
| 145 |
Provides content for Common Mistakes section.
|
|
@@ -161,6 +181,11 @@ class StreamlitTutorial:
|
|
| 161 |
- Keep it simple
|
| 162 |
"""
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
def _get_advanced_tips_content(self) -> str:
|
| 165 |
"""
|
| 166 |
Provides content for Advanced Tips section.
|
|
@@ -201,6 +226,9 @@ class StreamlitTutorial:
|
|
| 201 |
|
| 202 |
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
def get_text_elements(self) -> List[Tuple[str, str]]:
|
| 205 |
"""
|
| 206 |
Provides collection of text element examples with corresponding code.
|
|
@@ -219,6 +247,12 @@ class StreamlitTutorial:
|
|
| 219 |
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 220 |
]
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 223 |
"""
|
| 224 |
Provides collection of input widget examples with corresponding code.
|
|
@@ -236,6 +270,12 @@ class StreamlitTutorial:
|
|
| 236 |
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 237 |
]
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
def render_text_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 240 |
"""
|
| 241 |
Renders text element examples in the specified column.
|
|
@@ -258,6 +298,10 @@ class StreamlitTutorial:
|
|
| 258 |
with st.container(border=True):
|
| 259 |
exec(code)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 262 |
"""
|
| 263 |
Renders input widget examples in the specified column.
|
|
@@ -280,6 +324,10 @@ class StreamlitTutorial:
|
|
| 280 |
with st.container(border=True):
|
| 281 |
exec(code)
|
| 282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
def render_help_section(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 284 |
"""
|
| 285 |
Renders comprehensive help content in the specified column.
|
|
@@ -315,75 +363,89 @@ class StreamlitTutorial:
|
|
| 315 |
with st.expander("๐ Advanced Tips"):
|
| 316 |
st.markdown(self._get_advanced_tips_content())
|
| 317 |
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
"""
|
| 321 |
-
Renders interactive coding playground
|
| 322 |
-
|
| 323 |
-
Creates environment for hands-on practice.
|
| 324 |
|
| 325 |
Arguments:
|
| 326 |
-
col: Streamlit column object for
|
| 327 |
-
snippets: Dictionary of code snippets
|
| 328 |
-
|
| 329 |
-
Returns:
|
| 330 |
-
None
|
| 331 |
"""
|
| 332 |
with col:
|
| 333 |
st.markdown("### ๐ป Practice Playground")
|
| 334 |
|
| 335 |
-
# Code
|
| 336 |
code_input = st.text_area(
|
| 337 |
"Try Streamlit commands:",
|
| 338 |
-
|
| 339 |
height=200,
|
|
|
|
| 340 |
placeholder="Example:\nst.title('My Title')"
|
| 341 |
)
|
| 342 |
|
| 343 |
-
# Quick Snippets
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
cols = st.columns([4, 1])
|
| 347 |
-
with cols[0]:
|
| 348 |
-
selected_snippet = st.selectbox(
|
| 349 |
-
"Choose snippet:",
|
| 350 |
-
list(snippets.keys()),
|
| 351 |
-
label_visibility="collapsed"
|
| 352 |
-
)
|
| 353 |
-
with cols[1]:
|
| 354 |
-
if st.button("Add", use_container_width=True):
|
| 355 |
-
new_code = snippets[selected_snippet]
|
| 356 |
-
if code_input:
|
| 357 |
-
code_input += f"\n{new_code}"
|
| 358 |
-
else:
|
| 359 |
-
code_input = new_code
|
| 360 |
-
st.session_state.code_input = code_input
|
| 361 |
-
|
| 362 |
-
# Control Buttons
|
| 363 |
-
col1, col2, col3 = st.columns(3)
|
| 364 |
with col1:
|
| 365 |
-
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
| 367 |
with col2:
|
| 368 |
-
if st.button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
st.session_state.code_input = ""
|
| 370 |
-
|
| 371 |
-
with
|
| 372 |
-
if st.button("๐พ Copy",
|
| 373 |
st.code(code_input)
|
| 374 |
|
| 375 |
-
# Live
|
| 376 |
st.markdown("#### ๐จ Live Output")
|
| 377 |
with st.container(border=True):
|
| 378 |
-
if code_input:
|
| 379 |
try:
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
| 381 |
except Exception as e:
|
| 382 |
st.error(f"Error: {str(e)}")
|
| 383 |
|
| 384 |
-
# Tips
|
| 385 |
with st.expander("๐ก Tips & Help"):
|
| 386 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
def get_topic_tips(self, topic: str) -> str:
|
| 389 |
"""
|
|
@@ -415,6 +477,9 @@ class StreamlitTutorial:
|
|
| 415 |
}
|
| 416 |
return tips.get(topic, "Tips coming soon!")
|
| 417 |
|
|
|
|
|
|
|
|
|
|
| 418 |
def run(self) -> None:
|
| 419 |
"""
|
| 420 |
Main execution method for the Streamlit Tutorial application.
|
|
@@ -444,6 +509,8 @@ class StreamlitTutorial:
|
|
| 444 |
self.render_help_section(cols[1])
|
| 445 |
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 446 |
|
|
|
|
|
|
|
| 447 |
if __name__ == "__main__":
|
| 448 |
app = StreamlitTutorial()
|
| 449 |
app.run()
|
|
|
|
| 18 |
self.init_session_state()
|
| 19 |
self.setup_styles()
|
| 20 |
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def init_session_state(self) -> None:
|
| 24 |
"""
|
| 25 |
Initializes session state variables for persistent data storage.
|
|
|
|
| 33 |
st.session_state.code_input = ""
|
| 34 |
st.session_state.current_topic = "Basic Text Elements"
|
| 35 |
|
| 36 |
+
|
| 37 |
+
|
| 38 |
def setup_styles(self) -> None:
|
| 39 |
"""
|
| 40 |
Configures custom CSS styles for the application.
|
|
|
|
| 67 |
|
| 68 |
# Add these helper methods in the StreamlitTutorial class
|
| 69 |
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
def _get_basic_concepts_content(self) -> str:
|
| 74 |
"""
|
| 75 |
Provides content for Basic Concepts section.
|
|
|
|
| 91 |
4. Colored text
|
| 92 |
"""
|
| 93 |
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
def _get_best_practices_content(self) -> str:
|
| 100 |
"""
|
| 101 |
Provides content for Best Practices section.
|
|
|
|
| 120 |
```
|
| 121 |
"""
|
| 122 |
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
def _get_examples_content(self) -> str:
|
| 127 |
"""
|
| 128 |
Provides content for Examples section.
|
|
|
|
| 155 |
```
|
| 156 |
"""
|
| 157 |
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
def _get_common_mistakes_content(self) -> str:
|
| 164 |
"""
|
| 165 |
Provides content for Common Mistakes section.
|
|
|
|
| 181 |
- Keep it simple
|
| 182 |
"""
|
| 183 |
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
|
| 189 |
def _get_advanced_tips_content(self) -> str:
|
| 190 |
"""
|
| 191 |
Provides content for Advanced Tips section.
|
|
|
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
|
| 232 |
def get_text_elements(self) -> List[Tuple[str, str]]:
|
| 233 |
"""
|
| 234 |
Provides collection of text element examples with corresponding code.
|
|
|
|
| 247 |
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 248 |
]
|
| 249 |
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
|
| 256 |
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 257 |
"""
|
| 258 |
Provides collection of input widget examples with corresponding code.
|
|
|
|
| 270 |
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 271 |
]
|
| 272 |
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
|
| 279 |
def render_text_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 280 |
"""
|
| 281 |
Renders text element examples in the specified column.
|
|
|
|
| 298 |
with st.container(border=True):
|
| 299 |
exec(code)
|
| 300 |
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
|
| 305 |
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 306 |
"""
|
| 307 |
Renders input widget examples in the specified column.
|
|
|
|
| 324 |
with st.container(border=True):
|
| 325 |
exec(code)
|
| 326 |
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
|
| 331 |
def render_help_section(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 332 |
"""
|
| 333 |
Renders comprehensive help content in the specified column.
|
|
|
|
| 363 |
with st.expander("๐ Advanced Tips"):
|
| 364 |
st.markdown(self._get_advanced_tips_content())
|
| 365 |
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def render_playground(self, col: st.delta_generator.DeltaGenerator, snippets: Dict[str, str]) -> None:
|
| 372 |
"""
|
| 373 |
+
Renders interactive coding playground with live execution.
|
| 374 |
+
Manages code input, snippets, and real-time output display.
|
|
|
|
| 375 |
|
| 376 |
Arguments:
|
| 377 |
+
col: Streamlit column object for rendering
|
| 378 |
+
snippets: Dictionary of available code snippets
|
|
|
|
|
|
|
|
|
|
| 379 |
"""
|
| 380 |
with col:
|
| 381 |
st.markdown("### ๐ป Practice Playground")
|
| 382 |
|
| 383 |
+
# Code input area with session state
|
| 384 |
code_input = st.text_area(
|
| 385 |
"Try Streamlit commands:",
|
| 386 |
+
key="playground_input",
|
| 387 |
height=200,
|
| 388 |
+
value=st.session_state.get('code_input', ''),
|
| 389 |
placeholder="Example:\nst.title('My Title')"
|
| 390 |
)
|
| 391 |
|
| 392 |
+
# Quick Snippets
|
| 393 |
+
st.markdown("#### Quick Snippets")
|
| 394 |
+
col1, col2 = st.columns([3, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
with col1:
|
| 396 |
+
selected_snippet = st.selectbox(
|
| 397 |
+
"Choose snippet:",
|
| 398 |
+
list(snippets.keys()),
|
| 399 |
+
key="snippet_select"
|
| 400 |
+
)
|
| 401 |
with col2:
|
| 402 |
+
if st.button("Add", key="add_snippet"):
|
| 403 |
+
new_code = snippets[selected_snippet]
|
| 404 |
+
if 'code_input' not in st.session_state:
|
| 405 |
+
st.session_state.code_input = new_code
|
| 406 |
+
else:
|
| 407 |
+
st.session_state.code_input = st.session_state.code_input + "\n" + new_code
|
| 408 |
+
st.experimental_rerun()
|
| 409 |
+
|
| 410 |
+
# Control buttons
|
| 411 |
+
run_col, reset_col, copy_col = st.columns(3)
|
| 412 |
+
|
| 413 |
+
with run_col:
|
| 414 |
+
run = st.button("โถ๏ธ Run", key="run_button")
|
| 415 |
+
with reset_col:
|
| 416 |
+
if st.button("๐ Reset", key="reset_button"):
|
| 417 |
st.session_state.code_input = ""
|
| 418 |
+
st.experimental_rerun()
|
| 419 |
+
with copy_col:
|
| 420 |
+
if st.button("๐พ Copy", key="copy_button"):
|
| 421 |
st.code(code_input)
|
| 422 |
|
| 423 |
+
# Live output section
|
| 424 |
st.markdown("#### ๐จ Live Output")
|
| 425 |
with st.container(border=True):
|
| 426 |
+
if run and code_input:
|
| 427 |
try:
|
| 428 |
+
# Create a new local namespace for execution
|
| 429 |
+
local_dict = {}
|
| 430 |
+
# Execute in the local namespace
|
| 431 |
+
exec(code_input, globals(), local_dict)
|
| 432 |
except Exception as e:
|
| 433 |
st.error(f"Error: {str(e)}")
|
| 434 |
|
| 435 |
+
# Tips & Help
|
| 436 |
with st.expander("๐ก Tips & Help"):
|
| 437 |
+
st.markdown("""
|
| 438 |
+
**Quick Tips:**
|
| 439 |
+
- Type or paste Streamlit commands
|
| 440 |
+
- Use snippets for quick start
|
| 441 |
+
- Click Run to see results
|
| 442 |
+
- Reset to clear all code
|
| 443 |
+
""")
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
|
| 449 |
|
| 450 |
def get_topic_tips(self, topic: str) -> str:
|
| 451 |
"""
|
|
|
|
| 477 |
}
|
| 478 |
return tips.get(topic, "Tips coming soon!")
|
| 479 |
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
|
| 483 |
def run(self) -> None:
|
| 484 |
"""
|
| 485 |
Main execution method for the Streamlit Tutorial application.
|
|
|
|
| 509 |
self.render_help_section(cols[1])
|
| 510 |
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 511 |
|
| 512 |
+
|
| 513 |
+
|
| 514 |
if __name__ == "__main__":
|
| 515 |
app = StreamlitTutorial()
|
| 516 |
app.run()
|