# Creating a Data-Driven Web Application with Python and Streamlit
In this article, we will explore how to create a simple data-driven web application using Python and the Streamlit library. We will start by setting up the basic structure of our application, including the header and content sections.
## Setting Up the Header Section
The header section is indicated by a hash tag followed by the text "heading 1". This means that this line is a heading type 1, which will be displayed as a big text. Here's an example:
```
# Heading 1
This is a heading type 1 and it's gonna be a big text and then it's going to be an ordinary text saying shown are the stop closing price and volume of Google and then here in this blocks of code I've taken from the towards data science article so you want to check that already go out and give this article a clap and so I extracted some lines from this article and so this line of code will be the ticker symbol of Google and so it is goog l
```
As you can see, this is in markdown language and with the hash tag here it is indicating that this line is a heading type 1. This means that it's gonna be displayed as a big text and then it's going to be an ordinary text saying shown are the stop closing price and volume of Google.
## Displaying Historical Stock Data
We will use the `towards data science` article to extract some lines of code and display historical stock data for Google. Here's how we can do it:
```
# Ticker symbol
import pandas as pd
from yfinance import download
ticker = 'googl' # ticker symbol for Google
# Historical data
df = download(ticker, period='1y', start='2010-05-31', end='2020-05-31')
print(df.head())
```
This code will retrieve the historical stock price and volume of Google for the past year, starting from May 31st, 2010, to May 31st, 2020.
## Customizing the Web Application
We can customize our web application by editing the contents of the file. We can add more features, change the design, or even update the data in real-time. Here's an example of how we can modify the code to display a two-line chart for closing price and volume:
```
# Customize the web application
import streamlit as st
st.header('Closing Price')
st.write('**Closing Price**')
st.plot(df['Close'].values, label='Closing Price')
st.header('Volume')
st.write('*Volume*')
st.plot(df['Volume'].values, label='Volume')
```
This code will display two line charts for closing price and volume. We can customize the chart further by adding more features or changing the design.
## Running the Web Application
To run our web application, we need to use the Streamlit library to create a server. Here's an example of how we can do it:
```
# Run the web application
import streamlit as st
st.title('My Stock Price Application')
st.write('This is my stock price application.')
if __name__ == '__main__':
st.run()
```
This code will spawn up a web server and display our web application. We can run this code by saving it in a file, opening the terminal, navigating to the directory where we saved the file, and running `streamlit run my_app.py`.
## Updating the Web Application
One of the best features of Streamlit is its ability to update the contents of the file automatically when we make changes. This means that if we edit our code and save it, the web application will be updated in real-time without requiring us to restart the server.
To enable this feature, we need to select "Always" for both "Detect changes" and "Rerun on save". Here's an example of how we can do it:
```
# Update the web application
import streamlit as st
st.title('My Stock Price Application')
st.write('This is my stock price application.')
if __name__ == '__main__':
st.run()
```
By selecting "Always" for both "Detect changes" and "Rerun on save", we can update our web application automatically when we make changes to the code.
## Customizing the Markdown Style
We can customize the markdown style in Streamlit by using various formatting options. Here's an example of how we can do it:
```
# Customize the markdown style
import streamlit as st
st.header('### Heading 2')
st.write('*This is a *italic* text.')
if __name__ == '__main__':
st.run()
```
In this example, we used the `###` format to create a heading type 2, and we also used italic text by wrapping it in asterisks.
## Adding Tables
We can add tables to our web application using various formatting options. Here's an example of how we can do it:
```
# Add tables
import streamlit as st
st.header('### Table')
st.write('| Column 1 | Column 2 |')
st.write('| --- | --- |')
for i in range(5):
st.write('|', i, '|', i+1, '|')
if __name__ == '__main__':
st.run()
```
In this example, we created a table by using the pipe (`|`) character to separate the columns and rows.
## Displaying Multiple Lines of Code
We can display multiple lines of code in our web application by separating them with line breaks. Here's an example of how we can do it:
```
# Display multiple lines of code
import streamlit as st
st.header('### Code Block')
st.write('# Line 1')
st.write('# Line 2')
st.write('# Line 3')
if __name__ == '__main__':
st.run()
```
In this example, we used the `#` symbol to create a comment, and we also used line breaks to separate each line of code.