Py.Cafe

akarsh-xc/

streamlit-on-py-cafe

Streamlit on Py.cafe

DocsPricing
  • app.py
  • requirements.txt
app.py
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
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from datetime import date

# Set up the app title and layout
st.title("📈 Financial Dashboard")

# Sidebar inputs
st.sidebar.header("Stock Selection")
symbol = st.sidebar.text_input("Enter Stock Symbol", value="AAPL")

st.sidebar.header("Date Range")
start_date = st.sidebar.date_input("Start Date", date(2023, 1, 1))
end_date = st.sidebar.date_input("End Date", date.today())

# Fetch stock data function
@st.cache_data
def fetch_stock_data(symbol, start, end):
    return yf.download(symbol, start=start, end=end)

# Fetch and display the data
if st.sidebar.button("Fetch Data"):
    try:
        df = fetch_stock_data(symbol, start_date, end_date)
        
        if df.empty:
            st.error("No data found for the selected stock symbol and date range.")
        else:
            st.success(f"Showing data for {symbol}")
            
            # Display raw data
            st.subheader(f"Raw Data for {symbol}")
            st.dataframe(df.tail(10))

            # Plot the candlestick chart
            st.subheader(f"{symbol} Stock Price Chart")
            fig = go.Figure(data=[
                go.Candlestick(
                    x=df.index,
                    open=df['Open'],
                    high=df['High'],
                    low=df['Low'],
                    close=df['Close'],
                    name=symbol
                )
            ])

            fig.update_layout(
                title=f"{symbol} Stock Prices",
                xaxis_title="Date",
                yaxis_title="Price (USD)",
                xaxis_rangeslider_visible=True
            )

            st.plotly_chart(fig)
    
    except Exception as e:
        st.error(f"An error occurred: {e}")