Spaces:
Sleeping
Sleeping
Create app_utils.py
Browse files- app_utils.py +32 -0
app_utils.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from data_fetcher import get_stocks_from_index
|
| 4 |
+
from stock_analysis import get_stock_graph_and_info
|
| 5 |
+
|
| 6 |
+
def validate_date(date_string):
|
| 7 |
+
try:
|
| 8 |
+
return datetime.strptime(date_string, "%Y-%m-%d").date()
|
| 9 |
+
except ValueError:
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
def update_stock_options(index):
|
| 13 |
+
stocks = get_stocks_from_index(index)
|
| 14 |
+
return gr.Dropdown(choices=stocks)
|
| 15 |
+
|
| 16 |
+
def process_inputs(*args):
|
| 17 |
+
idx, stock, interval, graph_type, forecast_method, start_date, end_date = args
|
| 18 |
+
|
| 19 |
+
start = validate_date(start_date)
|
| 20 |
+
end = validate_date(end_date)
|
| 21 |
+
|
| 22 |
+
if start is None or end is None:
|
| 23 |
+
return gr.Textbox(value="Invalid date format. Please use YYYY-MM-DD."), None
|
| 24 |
+
|
| 25 |
+
if start > end:
|
| 26 |
+
return gr.Textbox(value="Start date must be before end date."), None
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
return get_stock_graph_and_info(idx, stock, interval, graph_type, forecast_method, start, end)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
error_message = f"An error occurred: {str(e)}"
|
| 32 |
+
return gr.Textbox(value=error_message), None
|