File size: 2,769 Bytes
28e7fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cc429d
28e7fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
9aae20a
 
28e7fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
6419007
28e7fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cc429d
28e7fd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)