2026-04-06 15:25:53 +02:00

105 lines
4.3 KiB
Python

import streamlit as st
from pathlib import Path
from backend.managers.file_manager import FileManager
fm = FileManager()
def render_filetree(tree, parent_path=Path()):
for name, content in sorted(tree.items()):
full_path = parent_path / name
if isinstance(content, dict): # Directory
with st.sidebar.expander(f"📁 {name}", expanded=False):
render_filetree(content, full_path)
else: # File
cols= st.sidebar.columns([3, 1])
with cols[0]:
if st.sidebar.button(f"📄 {name}", key=str(full_path)):
abs_path = fm.base_path / full_path
if str(abs_path) not in st.session_state.open_files:
st.session_state.open_files.append(str(abs_path))
st.session_state.active_file = str(abs_path)
with cols[1]:
show_options_key = f"show_options_{full_path}"
if st.button("...", key=show_options_key):
st.session_state[show_options_key] = not st.session_state.get(show_options_key, False)
if st.session_state.get(show_options_key, False):
action = st.selectbox("", ["", "Rename", "Delete"], key=f"action_{full_path}", index=0)
if action == "Delete":
st.warning("Are you sure you want to delete this file?")
if st.button("Yes, delete file", key=f"confirm_delete_{full_path}"):
try:
(fm.base_path / full_path).unlink()
st.experimental_rerun()
except Exception as e:
st.error(f"Error deleting file: {e}")
elif action == "Rename":
new_name = st.text_input("New Name", key=f"rename_{full_path}")
if "/" in new_name or "\\" in new_name:
st.warning("Do not include slashes in names!")
elif new_name:
new_path = (fm.base_path / full_path.parent / new_name)
try:
(fm.base_path / full_path).rename(new_path)
st.experimental_rerun()
except Exception as e:
st.error(f"Error renaming file: {e}")
def render_sidebar():
st.sidebar.title("Navigation")
navigation_section = st.sidebar.container()
file_explorer_section = st.sidebar.container()
with navigation_section:
options = ["Code Editor", "Chat with AI Assistant"]
choice = []
for option in options:
if st.sidebar.checkbox(option):
choice.append(option)
with file_explorer_section:
st.sidebar.subheader("File Explorer")
tree = fm.get_file_tree()
if not tree:
st.sidebar.info("Workspace is empty.")
else:
render_filetree(tree)
st.sidebar.markdown("---")
st.sidebar.write("Manage Files & Folders")
# Create Folder
new_folder_name = st.sidebar.text_input("New Folder Name", key="new_folder_name")
if "/" in new_folder_name or "\\" in new_folder_name:
st.warning("Do not include slashes in names!")
elif st.sidebar.button("Create Folder", key="create_folder") and new_folder_name:
new_folder_path = fm.base_path / new_folder_name
new_folder_path.mkdir(exist_ok=True)
st.success(f"Folder '{new_folder_name}' created successfully!")
st.experimental_rerun()
# Create File
new_file_name = st.sidebar.text_input("New File Name", key="new_file_name")
if "/" in new_file_name or "\\" in new_file_name:
st.warning("Do not include slashes in names!")
elif st.sidebar.button("Create File", key="create_file") and new_file_name:
new_file_path = fm.base_path / new_file_name
new_file_path.touch(exist_ok=True)
st.success(f"File '{new_file_name}' created successfully!")
st.experimental_rerun()
return choice
if __name__ == "__main__":
render_sidebar()