Building data science web application with Python

Time:2021-1-13

By Chanin nantasenamat
Compile VK
Source: towards Data Science

In this article, I’ll show you how to use itstreamlitPython library can quickly build a simple data-driven web application with only a few lines of code.

As a data scientist or machine learning engineer, it is very important to be able to deploy our data science projects. Traditional deployment of machine learning models using frameworks such as Django or flask can be a difficult and / or time-consuming task.

Overview of the stock web application we are building

Today, we’ll build a simple web application to display stock prices and volumes. This will require the use of two Python libraries, streamlit and yfinance.

Conceptually, the app will retrieve historical market data from Yahoo and get funding information from the yfinance library. This data is saved in the dataframe, and streamlit will use it as an input parameter to display it as a line chart.

Install prerequisite Libraries

In this tutorial, we will use two Python libraries that need to be installed. These include streamlit and yfinance. You can easily install streamlit by using the PIP install command below:

pip install streamlit

Do the same for yfinance, as follows:

pip install yfinance

Code of web application

Let’s look at the code for the web application we’re building today. You’ll find that there are less than 20 lines of code (that is, if you don’t calculate comments, reduce the code to 14 lines, three of which are empty lines for aesthetic purposes).

import yfinance as yf
import streamlit as st

st.write("""
#Simple stock price app
Shown are the stock closing price and volume of Google!
""")

# https://towardsdatascience.com/how-to-get-stock-data-using-python-c0de1df17e75
#Define stock code
tickerSymbol = 'GOOGL'
#Get the data of this stock
tickerData = yf.Ticker(tickerSymbol)
#Get a historical price for this stock
tickerDf = tickerData.history(period='1d', start='2010-5-31', end='2020-5-31')

st.line_chart(tickerDf.Close)
st.line_chart(tickerDf.Volume)

Line by line interpretation of code

Let’s take a moment to understand the code above.

  • Lines 1 and 2

Import yfinance and alias YF, import streamlit and alias St.

  • 4-7 lines

use st.write () function prints out text. The printed text is written in markdown format.

  • Line 9-16

    Use yfinance library to retrieve historical market data from Yahoo.

    • Line 11 – define the stock code as Googl.
    • Line 13 – use yf.Ticker The () function, as the name suggests, allows access to stock code data. Note that tickerdata is a tickerdata object. If we run tickerdata as a command, we will get the following output yfinance.Ticker object。
    • Line 15 – create the tickerdf data frame and define the date range (from May 31, 2010 to May 31, 2020) and time period (1 day).

  • Line 18-19

use st.line_ The chart() function draws a line chart (defined on line 15)CloseandVolumeColumn).

Running web applications

Save the code to a file named me app.py , start the command prompt (or power shell in Microsoft Windows) and run the following command:

streamlit run myapp.py

Next, we will see the following message:

> streamlit run myapp.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://10.0.0.11:8501

In a short time, an Internet browser window will pop up and guide you to the created web application http://localhost : 8501 as shown below.

You’ve created your first web application in Python!


Customizing Web Applications

Well, you may want to customize the interface of this web application. Please see the following code.

import yfinance as yf
import streamlit as st

st.write("""
# Simple Stock Price App
Shown are the stock **closing price** and ***volume*** of Google!
""")

# https://towardsdatascience.com/how-to-get-stock-data-using-python-c0de1df17e75
#Define stock code
tickerSymbol = 'GOOGL'
#Get the data of this stock
tickerData = yf.Ticker(tickerSymbol)
#Get a historical price for this stock
tickerDf = tickerData.history(period='1d', start='2010-5-31', end='2020-5-31')

st.write("""
## Closing Price
""")
st.line_chart(tickerDf.Close)
st.write("""
## Volume
""")

Let’s take a moment to understand the code above.

  • Line 6

Please note that we have bold “closing price”. Also note that we make the word “volume” bold and italicized by using three asterisks before and after the word.

  • Lines 18-20 and 22-25

Here, we add a title in markdown format before the closing price and trading volume chart.

Now we have an updated web application.

Link to the original text:https://towardsdatascience.com/how-to-build-a-data-science-web-app-in-python-61d1bed65020

Welcome to panchuang AI blog:
http://panchuang.net/

Sklearn machine learning official Chinese document:
http://sklearn123.com/

Welcome to pancreato blog Resource Hub:
http://docs.panchuang.net/