Configuration
We first import the necessary libraries, including yfinance, to retrieve the stock data.
import reflex as rx
from reflex_ag_grid import ag_grid
import yfinance as yf
from datetime import datetime, timedelta
import pandas as pd
Obtaining and transforming data
Next, we define the State class, which contains the state and logic of the application. He fetch_stock_data
The function retrieves stock data for the specified companies and transforms it into a format suitable for display on AG Grid. We call this function when clicking a button, binding the on_click
button trigger to this function status.
We define state variables, any field in your application that can change over time (a state variable is represented directly in the application interface).
He data
The state variable stores the raw stock data obtained from Yahoo Finance. We transform this data to round the values and store it as a list of dictionaries, which is the format AG Grid expects. The transformed data is sorted by date and ticker in descending order and stored in the dict_data
state variable.
He datetime_now
The state variable stores the current date and time that the data was obtained.
# The list of companies to fetch data for
companies = ("AAPL", "MSFT", "GOOGL", "AMZN", "META")class State(rx.State):
# The data fetched from Yahoo Finance
data: pd.DataFrame
# The data to be displayed in the AG Grid
dict_data: list(dict) = (\{})
# The datetime of the current fetched data
datetime_now: datetime = datetime.now()
def fetch_stock_data(self):
self.datetime_now = datetime.now()
start_date = self.datetime_now - timedelta(days=180)
# Fetch data for all tickers in a single download
self.data = yf.download(companies, start=start_date, end=self.datetime_now, group_by='ticker')
rows = ()
for ticker in companies:
# Check if the DataFrame has a multi-level column index (for multiple tickers)
if isinstance(self.data.columns, pd.MultiIndex):
ticker_data = self.data(ticker) # Select the data for the current ticker
else:
ticker_data = self.data # If only one ticker, no multi-level index exists
for date, row in ticker_data.iterrows():
rows.append({
"ticker": ticker,
"date": date.strftime("%Y-%m-%d"),
"open": round(row("Open"), 2),
"high": round(row("High"), 2),
"mid": round((row("High") + row("Low")) / 2, 2),
"low": round(row("Low"), 2),
"close": round(row("Close"), 2),
"volume": int(row("Volume")),
})
self.dict_data = sorted(rows, key=lambda x: (x("date"), x("ticker")), reverse=True)
rx.button(
"Fetch Latest Data",
on_click=State.fetch_stock_data,
)