AISEAICodeEditor/src/ui/editor.py
2026-05-26 20:58:25 +02:00

54 lines
1.6 KiB
Python

from streamlit_ace import st_ace
import streamlit as st
class CodeEditor:
def detect_language(self, filename: str) -> str:
mapping = {
".py": "python",
".js": "javascript",
".html": "html",
".css": "css",
".json": "json",
".md": "markdown",
}
for ext, lang in mapping.items():
if filename.endswith(ext):
return lang
return "text"
def display_code(self, code_content: str, filename: str = "") -> tuple:
"""
Renders a toolbar row (Apply is built into st_ace, Run is next to it)
then the ACE editor below.
Returns (new_value, run_clicked) where new_value is None until Apply is hit.
"""
language = self.detect_language(filename)
# --- Toolbar: filename label + Run button aligned right ---
label_col, run_col = st.columns([6, 1])
with label_col:
st.caption(f" {filename.split('/')[-1] or filename}" if filename else "")
with run_col:
run_clicked = st.button(
"▶ RUN CODE",
key=f"run_{filename}",
type="primary",
use_container_width=True
)
new_value = st_ace(
value=code_content,
language=language,
theme="chrome",
key=f"ace_{filename}",
font_size=14,
tab_size=4,
wrap=False,
auto_update=False,
show_gutter=True,
show_print_margin=False,
)
return new_value, run_clicked