Getting Began with Highly effective Knowledge Tables in Your Python Net Apps | by Tom Gotsman | Oct, 2024

[ad_1]

Setup

First we import the mandatory libraries, together with yfinance for fetching the inventory knowledge.

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 knowledge

Subsequent, we outline the State class, which accommodates the appliance’s state and logic. The fetch_stock_data operate fetches inventory knowledge for the desired corporations and transforms it right into a format appropriate for show in AG Grid. We name this operate when clicking on a button, by linking the on_click set off of the button to this state operate.

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

The knowledge state variable shops the uncooked inventory knowledge fetched from Yahoo Finance. We remodel this knowledge to around the values and retailer it as a listing of dictionaries, which is the format that AG Grid expects. The remodeled knowledge is sorted by date and ticker in descending order and saved within the dict_data state variable.

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

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

class State(rx.State):
# The information fetched from Yahoo Finance
knowledge: pd.DataFrame
# The information to be displayed within the AG Grid
dict_data: checklist[dict] = [{}]
# The datetime of the present fetched knowledge
datetime_now: datetime = datetime.now()

def fetch_stock_data(self):
self.datetime_now = datetime.now()
start_date = self.datetime_now - timedelta(days=180)

# Fetch knowledge for all tickers in a single obtain
self.knowledge = yf.obtain(corporations, begin=start_date, finish=self.datetime_now, group_by='ticker')
rows = []
for ticker in corporations:
# Test if the DataFrame has a multi-level column index (for a number of tickers)
if isinstance(self.knowledge.columns, pd.MultiIndex):
ticker_data = self.knowledge[ticker] # Choose the information for the present ticker
else:
ticker_data = self.knowledge # 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": spherical(row["Open"], 2),
"excessive": spherical(row["High"], 2),
"mid": spherical((row["High"] + row["Low"]) / 2, 2),
"low": spherical(row["Low"], 2),
"shut": spherical(row["Close"], 2),
"quantity": int(row["Volume"]),
})

self.dict_data = sorted(rows, key=lambda x: (x["date"], x["ticker"]), reverse=True)

rx.button(
"Fetch Newest Knowledge",
on_click=State.fetch_stock_data,
)

[ad_2]
Tom Gotsman
2024-10-06 17:02:01
Source hyperlink:https://towardsdatascience.com/getting-started-with-powerful-data-tables-in-your-python-web-apps-1e48dc44caf2?source=rss—-7f60cf5620c9—4

Similar Articles

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular