Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import streamlit_ace as st_ace | |
| from utils import execute_code, export_session | |
| from chatbot import render_chatbot | |
| import json | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Interactive Code Editor with AI Assistant", | |
| page_icon="π»", | |
| layout="wide" | |
| ) | |
| # Initialize session state | |
| if 'code_output' not in st.session_state: | |
| st.session_state.code_output = "" | |
| if 'error_output' not in st.session_state: | |
| st.session_state.error_output = "" | |
| # Title and description | |
| st.title("π» Interactive Code Editor") | |
| st.markdown(""" | |
| Write, execute, and export Python code in this interactive editor. | |
| The editor supports syntax highlighting and autocompletion. | |
| """) | |
| # Create main layout with three columns | |
| col1, col2= st.columns([1, 1]) | |
| with col1: | |
| # Code Editor Section | |
| st.subheader("Code Editor") | |
| code = st_ace.st_ace( | |
| placeholder="Write your Python code here...", | |
| language="python", | |
| theme="chrome", | |
| keybinding="vscode", | |
| font_size=14, | |
| min_lines=25, | |
| key="ace_editor", | |
| show_gutter=True, | |
| wrap=True, | |
| show_print_margin=True, | |
| auto_update=True | |
| ) | |
| # Execute button and output | |
| if st.button("βΆοΈ Run Code", type="primary"): | |
| output, error, exception = execute_code(code) | |
| st.session_state.code_output = output | |
| st.session_state.error_output = error if error else exception | |
| # Display immediate output below the run button | |
| if st.session_state.code_output: | |
| st.text_area("Output", st.session_state.code_output, height=100) | |
| if st.session_state.error_output: | |
| st.error(st.session_state.error_output) | |
| with col2: | |
| # AI Assistant Section (Always visible) | |
| st.subheader("π€ Code Assistant") | |
| render_chatbot(code, st.session_state.code_output, st.session_state.error_output) | |
| # Export options at the bottom | |
| st.markdown("---") | |
| st.subheader("Export Options") | |
| # Create export data | |
| export_data = export_session( | |
| code, | |
| st.session_state.code_output, | |
| st.session_state.error_output | |
| ) | |
| # Export buttons in a more compact layout | |
| col1_export, col2_export= st.columns([5, 4]) | |
| with col1_export: | |
| st.download_button( | |
| "π Export as JSON", | |
| data=json.dumps(export_data, indent=2), | |
| file_name="code_session.json", | |
| mime="application/json" | |
| ) | |
| with col2_export: | |
| st.download_button( | |
| "π Export as Text", | |
| data=f"""# Code:\n{code}\n\n# Output:\n{st.session_state.code_output}\n\n# Errors:\n{st.session_state.error_output}""", | |
| file_name="code_session.txt", | |
| mime="text/plain" | |
| ) | |
| # Footer | |
| st.markdown(""" | |
| <div style='text-align: center'> | |
| <p>Built with Streamlit β€οΈ</p> | |
| </div> | |
| """, unsafe_allow_html=True) |