Spaces:
Sleeping
Sleeping
File size: 3,561 Bytes
5249394 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
import os
import streamlit as st
from tkinter import Tk, filedialog
import time
from utils.utils import analyze_directory_with_agents, load_css
class IntroductoryPage:
"""Handles directory selection and initial analysis."""
def __init__(self, agents):
self.agents = agents
self._init_session_state()
def _init_session_state(self):
required_state = {
'analysis_done': False,
'selected_dir': None,
'prev_dir': None,
'results': None
}
for key, val in required_state.items():
if key not in st.session_state:
st.session_state[key] = val
@staticmethod
def _select_directory():
"""Open system directory picker."""
root = Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
directory = filedialog.askdirectory(parent=root)
root.destroy()
return directory
def _process_directory(self):
"""Handle directory selection and analysis."""
directory = self._select_directory()
if not directory:
st.warning("No directory selected")
return
if not os.path.isdir(directory):
st.error("Invalid directory")
return
st.session_state.selected_dir = directory
self._analyze_directory(directory)
def _progress_bar_callback_func(
self,
current_idx: int,
file_path: str,
progress_value: int
) -> None:
"""
Callback function for `analyze_directory_with_agents`.
Updates the progress bar and status text with the current file being processed.
"""
if current_idx == 0:
self.status_text.markdown("🔍 Analyzing code structure...")
progress_value = progress_value if progress_value < 100 else 100
self.progress_bar.progress(current_idx * progress_value)
self.status_text.markdown(f"Processing {file_path}")
def _analyze_directory(self, directory):
"""Run analysis on selected directory with progress bar."""
self.progress_bar = st.progress(0)
self.status_text = st.empty()
try:
st.session_state.results = analyze_directory_with_agents(
directory, self.agents, verbose=True, callback_func = self._progress_bar_callback_func
)
st.session_state.analysis_done = True
st.rerun()
except Exception as e:
self.status_text.empty()
st.error(f"Analysis failed: {str(e)}")
st.session_state.analysis_done = False
st.session_state.selected_dir = None
def render(self):
"""Main entry point for introductory page."""
self._handle_state()
if not st.session_state.analysis_done:
self._render_selector()
def _render_selector(self):
"""Render file selection interface."""
load_css("./ui/styles.css")
if st.button("Select Directory", key="start_button"):
self._process_directory()
def _handle_state(self):
"""Manage session state transitions."""
if st.session_state.selected_dir != st.session_state.prev_dir:
st.session_state.analysis_done = False
st.session_state.prev_dir = st.session_state.selected_dir |