Spaces:
Sleeping
Sleeping
Make app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import streamlit_ace as st_ace
|
| 3 |
+
from utils import execute_code, export_session
|
| 4 |
+
from chatbot import render_chatbot
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Page configuration
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="Interactive Code Editor with AI Assistant",
|
| 10 |
+
page_icon="💻",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Initialize session state
|
| 15 |
+
if 'code_output' not in st.session_state:
|
| 16 |
+
st.session_state.code_output = ""
|
| 17 |
+
if 'error_output' not in st.session_state:
|
| 18 |
+
st.session_state.error_output = ""
|
| 19 |
+
|
| 20 |
+
# Title and description
|
| 21 |
+
st.title("💻 Interactive Code Editor")
|
| 22 |
+
st.markdown("""
|
| 23 |
+
Write, execute, and export Python code in this interactive editor.
|
| 24 |
+
The editor supports syntax highlighting and autocompletion.
|
| 25 |
+
""")
|
| 26 |
+
|
| 27 |
+
# Create main layout with three columns
|
| 28 |
+
col1, col2, col3 = st.columns([2, 1.5, 1.5])
|
| 29 |
+
|
| 30 |
+
with col1:
|
| 31 |
+
# Code Editor Section
|
| 32 |
+
st.subheader("Code Editor")
|
| 33 |
+
code = st_ace.st_ace(
|
| 34 |
+
placeholder="Write your Python code here...",
|
| 35 |
+
language="python",
|
| 36 |
+
theme="chrome",
|
| 37 |
+
keybinding="vscode",
|
| 38 |
+
font_size=14,
|
| 39 |
+
min_lines=25,
|
| 40 |
+
key="ace_editor",
|
| 41 |
+
show_gutter=True,
|
| 42 |
+
wrap=True,
|
| 43 |
+
auto_update=True,
|
| 44 |
+
show_print_margin=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Execute button and output
|
| 48 |
+
if st.button("▶️ Run Code", type="primary"):
|
| 49 |
+
output, error, exception = execute_code(code)
|
| 50 |
+
st.session_state.code_output = output
|
| 51 |
+
st.session_state.error_output = error if error else exception
|
| 52 |
+
|
| 53 |
+
# Display immediate output below the run button
|
| 54 |
+
if st.session_state.code_output:
|
| 55 |
+
st.text_area("Output", st.session_state.code_output, height=100)
|
| 56 |
+
if st.session_state.error_output:
|
| 57 |
+
st.error(st.session_state.error_output)
|
| 58 |
+
|
| 59 |
+
with col2, col3:
|
| 60 |
+
# AI Assistant Section (Always visible)
|
| 61 |
+
st.subheader("🤖 Code Assistant")
|
| 62 |
+
render_chatbot(code, st.session_state.code_output, st.session_state.error_output)
|
| 63 |
+
|
| 64 |
+
# Export options at the bottom
|
| 65 |
+
st.markdown("---")
|
| 66 |
+
st.subheader("Export Options")
|
| 67 |
+
|
| 68 |
+
# Create export data
|
| 69 |
+
export_data = export_session(
|
| 70 |
+
code,
|
| 71 |
+
st.session_state.code_output,
|
| 72 |
+
st.session_state.error_output
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Export buttons in a more compact layout
|
| 76 |
+
col1_export, col2_export, col3_export = st.columns([1, 1, 2])
|
| 77 |
+
|
| 78 |
+
with col1_export:
|
| 79 |
+
st.download_button(
|
| 80 |
+
"📄 Export as JSON",
|
| 81 |
+
data=json.dumps(export_data, indent=2),
|
| 82 |
+
file_name="code_session.json",
|
| 83 |
+
mime="application/json"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with col2_export:
|
| 87 |
+
st.download_button(
|
| 88 |
+
"📝 Export as Text",
|
| 89 |
+
data=f"""# Code:\n{code}\n\n# Output:\n{st.session_state.code_output}\n\n# Errors:\n{st.session_state.error_output}""",
|
| 90 |
+
file_name="code_session.txt",
|
| 91 |
+
mime="text/plain"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Footer
|
| 95 |
+
st.markdown("""
|
| 96 |
+
<div style='text-align: center'>
|
| 97 |
+
<p>Built with Streamlit ❤️</p>
|
| 98 |
+
</div>
|
| 99 |
+
""", unsafe_allow_html=True)
|