update code

This commit is contained in:
Carl Zh.
2024-11-05 11:40:32 -06:00
parent 90dc7e1448
commit 5595571596
2 changed files with 86 additions and 67 deletions

View File

@@ -28,51 +28,61 @@ def pythonx_lesson3():
st.header(":three: Application Demo") st.header(":three: Application Demo")
# print text formatted by HTML # print text formatted by HTML
# Display a styled title in HTML format
st.markdown( st.markdown(
"<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>", "<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>",
unsafe_allow_html=True) unsafe_allow_html=True)
st.write("Reference: [yfinance Package](https://aroussi.com/post/python-yahoo-finance)")
# Brief description of the app's purpose
st.subheader('Build an online application for tracing stock price history')
st.write( # Display a subheading in Markdown
'We will use the code learned from previous lecture to build an online application for tracing stock price' st.header("Check Stock History")
)
# print text in markdown # Define a list of popular stock symbols
st.markdown("## **Check Stock Information**") stock_list = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# a list of stock names # Create a dropdown menu for selecting a stock
stock_names = ['MSFT', 'AAPL', 'AMZN', 'GOOGL'] stock_name = st.selectbox('Select a stock to check', options=stock_list)
# select a stock to check
target_stock = st.selectbox('Select a stock to check', options=stock_names)
st.markdown("## **Check Stock Price History**") # Input for selecting the start date of stock data, default is January 1, 2024
# start date of the stock infomation, default is the first day of year 2024
start_date = st.date_input('Start Date', datetime(2024, 1, 1)) start_date = st.date_input('Start Date', datetime(2024, 1, 1))
# end date of the stock infomation, default is date of today # Input for selecting the end date of stock data, default is today
end_date = st.date_input("End Date") end_date = st.date_input("End Date")
# get today date # Store today's date for validation
today = date.today() today = date.today()
if st.button('Submit'):
# check valid date
if start_date > today or end_date > today:
st.write("## **Please select a valid date period.**")
else:
# download the stock data based on stock name, start/end date
data = yf.download(target_stock, start_date, end_date)
# show a progress bar
with st.spinner(text='In progress'):
fig = px.line(data, # Action to retrieve stock data when the 'Submit' button is clicked
x=data.index, if st.button('Submit'):
y=['Open', 'High', 'Low', 'Close'], # Check if selected dates are valid (not in the future)
title=target_stock + " Stock Price", if (start_date > today) or (end_date > today) or (start_date > end_date):
labels={ st.warning("Please select a valid date period.")
"value": "Stock Price ($)", else:
"variable": "Price Type" # Get data for the selected stock
}) stock = yf.Ticker(stock_name)
st.write(fig) # Retrieve historical data between selected dates
st.success('Done') stock_history = stock.history(start=start_date, end=end_date)
st.write("**Raw Data of Stock Price**")
# Display raw data of stock price
st.dataframe(stock_history)
# Plot stock price data (Open, High, Low, Close) using Plotly
fig = px.line(stock_history,
# use the index of the stock_history data as x-axis.
x=stock_history.index,
# plot data of columns ['Open', 'High', 'Low', 'Close']
y=['Open', 'High', 'Low', 'Close'],
title=stock_name + " Stock Price",
labels={
"value": "Stock Price ($)",
"variable": "Price Type"
})
# Display the generated plot in the app
st.write(fig)
# Display a success message upon completion
st.success('Done')
source_code = """ source_code = """
@@ -82,52 +92,61 @@ def pythonx_lesson3():
import yfinance as yf import yfinance as yf
import plotly.express as px import plotly.express as px
# print text formatted by HTML
# Display a styled title in HTML format
st.markdown( st.markdown(
"<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>", "<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>",
unsafe_allow_html=True) unsafe_allow_html=True)
st.write("Reference: [yfinance Package](https://aroussi.com/post/python-yahoo-finance)")
# Brief description of the app's purpose
st.subheader('Build an online application for tracing stock price history')
st.write( # Display a subheading in Markdown
'We will use the code learned from previous lecture to build an online application for tracing stock price' st.header("Check Stock History")
)
# print text in markdown # Define a list of popular stock symbols
st.markdown("## **Check Stock Information**") stock_list = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# a list of stock names # Create a dropdown menu for selecting a stock
stock_names = ['MSFT', 'AAPL', 'AMZN', 'GOOGL'] stock_name = st.selectbox('Select a stock to check', options=stock_list)
# select a stock to check
target_stock = st.selectbox('Select a stock to check', options=stock_names)
st.markdown("## **Check Stock Price History**") # Input for selecting the start date of stock data, default is January 1, 2024
# start date of the stock infomation, default is the first day of year 2024
start_date = st.date_input('Start Date', datetime(2024, 1, 1)) start_date = st.date_input('Start Date', datetime(2024, 1, 1))
# end date of the stock infomation, default is date of today # Input for selecting the end date of stock data, default is today
end_date = st.date_input("End Date") end_date = st.date_input("End Date")
# get today date # Store today's date for validation
today = date.today() today = date.today()
if st.button('Submit'):
# check valid date
if start_date > today or end_date > today:
st.write("## **Please select a valid date period.**")
else:
# download the stock data based on stock name, start/end date
data = yf.download(target_stock, start_date, end_date)
# show a progress bar
with st.spinner(text='In progress'):
fig = px.line(data, # Action to retrieve stock data when the 'Submit' button is clicked
x=data.index, if st.button('Submit'):
y=['Open', 'High', 'Low', 'Close'], # Check if selected dates are valid (not in the future)
title=target_stock + " Stock Price", if (start_date > today) or (end_date > today) or (start_date > end_date):
labels={ st.warning("Please select a valid date period.")
"value": "Stock Price ($)", else:
"variable": "Price Type" # Get data for the selected stock
}) stock = yf.Ticker(stock_name)
st.write(fig) # Retrieve historical data between selected dates
st.success('Done') stock_history = stock.history(start=start_date, end=end_date)
st.write("**Raw Data of Stock Price**")
# Display raw data of stock price
st.dataframe(stock_history)
# Plot stock price data (Open, High, Low, Close) using Plotly
fig = px.line(stock_history,
# use the index of the stock_history data as x-axis.
x=stock_history.index,
# plot data of columns ['Open', 'High', 'Low', 'Close']
y=['Open', 'High', 'Low', 'Close'],
title=stock_name + " Stock Price",
labels={
"value": "Stock Price ($)",
"variable": "Price Type"
})
# Display the generated plot in the app
st.write(fig)
# Display a success message upon completion
st.success('Done')
""" """