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")
# print text formatted by HTML
# Display a styled title in HTML format
st.markdown(
"<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>",
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(
'We will use the code learned from previous lecture to build an online application for tracing stock price'
)
# Display a subheading in Markdown
st.header("Check Stock History")
# print text in markdown
st.markdown("## **Check Stock Information**")
# Define a list of popular stock symbols
stock_list = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# a list of stock names
stock_names = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# select a stock to check
target_stock = st.selectbox('Select a stock to check', options=stock_names)
# Create a dropdown menu for selecting a stock
stock_name = st.selectbox('Select a stock to check', options=stock_list)
st.markdown("## **Check Stock Price History**")
# start date of the stock infomation, default is the first day of year 2024
# Input for selecting the start date of stock data, default is January 1, 2024
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")
# get today date
# Store today's date for validation
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,
x=data.index,
y=['Open', 'High', 'Low', 'Close'],
title=target_stock + " Stock Price",
labels={
"value": "Stock Price ($)",
"variable": "Price Type"
})
st.write(fig)
st.success('Done')
# Action to retrieve stock data when the 'Submit' button is clicked
if st.button('Submit'):
# Check if selected dates are valid (not in the future)
if (start_date > today) or (end_date > today) or (start_date > end_date):
st.warning("Please select a valid date period.")
else:
# Get data for the selected stock
stock = yf.Ticker(stock_name)
# Retrieve historical data between selected dates
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 = """
@@ -82,52 +92,61 @@ def pythonx_lesson3():
import yfinance as yf
import plotly.express as px
# print text formatted by HTML
# Display a styled title in HTML format
st.markdown(
"<h1 style='text-align:center; color:red;'> Sample Stock Price App. </h1>",
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(
'We will use the code learned from previous lecture to build an online application for tracing stock price'
)
# Display a subheading in Markdown
st.header("Check Stock History")
# print text in markdown
st.markdown("## **Check Stock Information**")
# Define a list of popular stock symbols
stock_list = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# a list of stock names
stock_names = ['MSFT', 'AAPL', 'AMZN', 'GOOGL']
# select a stock to check
target_stock = st.selectbox('Select a stock to check', options=stock_names)
# Create a dropdown menu for selecting a stock
stock_name = st.selectbox('Select a stock to check', options=stock_list)
st.markdown("## **Check Stock Price History**")
# start date of the stock infomation, default is the first day of year 2024
# Input for selecting the start date of stock data, default is January 1, 2024
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")
# get today date
# Store today's date for validation
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,
x=data.index,
y=['Open', 'High', 'Low', 'Close'],
title=target_stock + " Stock Price",
labels={
"value": "Stock Price ($)",
"variable": "Price Type"
})
st.write(fig)
st.success('Done')
# Action to retrieve stock data when the 'Submit' button is clicked
if st.button('Submit'):
# Check if selected dates are valid (not in the future)
if (start_date > today) or (end_date > today) or (start_date > end_date):
st.warning("Please select a valid date period.")
else:
# Get data for the selected stock
stock = yf.Ticker(stock_name)
# Retrieve historical data between selected dates
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')
"""