update code
This commit is contained in:
Binary file not shown.
@@ -28,53 +28,63 @@ 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'):
|
||||||
|
# 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'],
|
y=['Open', 'High', 'Low', 'Close'],
|
||||||
title=target_stock + " Stock Price",
|
title=stock_name + " Stock Price",
|
||||||
labels={
|
labels={
|
||||||
"value": "Stock Price ($)",
|
"value": "Stock Price ($)",
|
||||||
"variable": "Price Type"
|
"variable": "Price Type"
|
||||||
})
|
})
|
||||||
|
# Display the generated plot in the app
|
||||||
st.write(fig)
|
st.write(fig)
|
||||||
|
# Display a success message upon completion
|
||||||
st.success('Done')
|
st.success('Done')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
source_code = """
|
source_code = """
|
||||||
|
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
@@ -82,54 +92,63 @@ 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'):
|
||||||
|
# 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'],
|
y=['Open', 'High', 'Low', 'Close'],
|
||||||
title=target_stock + " Stock Price",
|
title=stock_name + " Stock Price",
|
||||||
labels={
|
labels={
|
||||||
"value": "Stock Price ($)",
|
"value": "Stock Price ($)",
|
||||||
"variable": "Price Type"
|
"variable": "Price Type"
|
||||||
})
|
})
|
||||||
|
# Display the generated plot in the app
|
||||||
st.write(fig)
|
st.write(fig)
|
||||||
|
# Display a success message upon completion
|
||||||
st.success('Done')
|
st.success('Done')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
st.divider()
|
st.divider()
|
||||||
st.subheader("**Source Code of the Sample App.**")
|
st.subheader("**Source Code of the Sample App.**")
|
||||||
|
|||||||
Reference in New Issue
Block a user