Spaces:
Sleeping
Sleeping
| import yfinance as yf | |
| import gradio as gr | |
| import pandas as pd | |
| import json | |
| def get_stock_history(symbol, period="1mo"): | |
| try: | |
| ticker = yf.Ticker(symbol.upper()) | |
| data = ticker.history(period=period) | |
| if data.empty: | |
| return f"No data found for {symbol.upper()}" | |
| return data.to_string() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def get_stock_info(symbol): | |
| try: | |
| ticker = yf.Ticker(symbol.upper()) | |
| info = ticker.info | |
| key_info = { | |
| 'Company': info.get('longName', 'N/A'), | |
| 'Symbol': info.get('symbol', 'N/A'), | |
| 'Current Price': f"${info.get('currentPrice', 'N/A')}", | |
| 'Market Cap': info.get('marketCap', 'N/A'), | |
| 'PE Ratio': info.get('trailingPE', 'N/A'), | |
| 'Sector': info.get('sector', 'N/A'), | |
| '52 Week High': f"${info.get('fiftyTwoWeekHigh', 'N/A')}", | |
| '52 Week Low': f"${info.get('fiftyTwoWeekLow', 'N/A')}" | |
| } | |
| return "\n".join([f"{k}: {v}" for k, v in key_info.items()]) | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def get_multiple_stocks(symbols_text): | |
| try: | |
| symbols = [s.strip().upper() for s in symbols_text.split(',') if s.strip()] | |
| if not symbols: | |
| return "Please enter at least one symbol" | |
| data = yf.download(symbols, period="5d", group_by='ticker') | |
| if data.empty: | |
| return "No data found for the provided symbols" | |
| return data.to_string() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Enhanced UI | |
| with gr.Blocks(title="YFinance API", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π YFinance Stock Market API") | |
| gr.Markdown("Get real-time stock data from Yahoo Finance") | |
| with gr.Tab("π Single Stock History"): | |
| with gr.Row(): | |
| hist_symbol = gr.Textbox(label="Stock Symbol", value="AAPL") | |
| hist_period = gr.Dropdown( | |
| choices=["1d", "5d", "1mo", "3mo", "6mo", "1y", "2y"], | |
| value="1mo", | |
| label="Time Period" | |
| ) | |
| hist_btn = gr.Button("Get History", variant="primary") | |
| hist_output = gr.Textbox(label="Historical Data", lines=12) | |
| hist_btn.click(get_stock_history, [hist_symbol, hist_period], hist_output) | |
| with gr.Tab("βΉοΈ Company Information"): | |
| info_symbol = gr.Textbox(label="Stock Symbol", value="MSFT") | |
| info_btn = gr.Button("Get Company Info", variant="primary") | |
| info_output = gr.Textbox(label="Company Details", lines=10) | |
| info_btn.click(get_stock_info, info_symbol, info_output) | |
| with gr.Tab("π Multiple Stocks"): | |
| multi_symbols = gr.Textbox( | |
| label="Stock Symbols (comma separated)", | |
| value="AAPL,MSFT,GOOGL", | |
| placeholder="Enter symbols like: AAPL,MSFT,TSLA" | |
| ) | |
| multi_btn = gr.Button("Get Multiple Stocks", variant="primary") | |
| multi_output = gr.Textbox(label="Multiple Stock Data", lines=15) | |
| multi_btn.click(get_multiple_stocks, multi_symbols, multi_output) | |
| demo.launch() | |