File size: 1,082 Bytes
cdc77d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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