from datetime import datetime import gradio as gr from data_fetcher import get_stocks_from_index from stock_analysis import get_stock_graph_and_info def validate_date(date_string): try: return datetime.strptime(date_string, "%Y-%m-%d").date() except ValueError: return None def update_stock_options(index): stocks = get_stocks_from_index(index) return gr.Dropdown(choices=stocks) def process_inputs(*args): idx, stock, interval, graph_type, forecast_method, start_date, end_date = args start = validate_date(start_date) end = validate_date(end_date) if start is None or end is None: return gr.Textbox(value="Invalid date format. Please use YYYY-MM-DD."), None if start > end: return gr.Textbox(value="Start date must be before end date."), None try: return get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start, end) except Exception as e: error_message = f"An error occurred: {str(e)}" return gr.Textbox(value=error_message), None