Getting Began with Powerful Data Tables in Your Python Web Apps

-

Setup

First we import the essential libraries, including yfinance for fetching 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

Fetching and remodeling data

Next, we define the State class, which incorporates the applying’s state and logic. The fetch_stock_data function fetches stock data for the required corporations and transforms it right into a format suitable for display in AG Grid. We call this function when clicking on a button, by linking the on_click trigger of the button to this state function.

We define state variables, any fields in your app which will change over time (A State Var is directly rendered into the frontend of the app).

The data state variable stores the raw stock data fetched from Yahoo Finance. We transform this data to around the values and store it as a listing of dictionaries, which is the format that AG Grid expects. The transformed data is sorted by date and ticker in descending order and stored within the dict_data state variable.

The datetime_now state variable stores the present datetime when the information was fetched.

# The list of corporations to fetch data for
corporations = ["AAPL", "MSFT", "GOOGL", "AMZN", "META"]

class State(rx.State):
# The information fetched from Yahoo Finance
data: pd.DataFrame
# The information to be displayed within the AG Grid
dict_data: list[dict] = [{}]
# The datetime of the present 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(corporations, start=start_date, end=self.datetime_now, group_by='ticker')
rows = []
for ticker in corporations:
# 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 information for the present ticker
else:
ticker_data = self.data # If just 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,
)
ASK ANA

What are your thoughts on this topic?
Let us know in the comments below.

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

0
Would love your thoughts, please comment.x
()
x