寫出你的第一個web app,幾行代碼就夠了

2021-02-12    分類: 網(wǎng)站建設(shè)

寫出你的第一個web app,幾行代碼就夠了

圖源:pixabay

無論數(shù)據(jù)科學(xué)家還是機器學(xué)習工程師,部署數(shù)據(jù)科學(xué)項目都是他們所需要的一項至關(guān)重要的能力。傳統(tǒng)部署機器學(xué)習模型的方法是利用Django、Flask等已經(jīng)建立好的框架,然而這種方法非常耗時,常常令人望而卻步。有沒有簡單一點的方法呢?

本文是基于筆者在YouTubevideo上發(fā)布的同名視頻創(chuàng)作的,二者搭配食用效果更佳。

股票web app概述

現(xiàn)在我們要構(gòu)建一個顯示股價與股票成交量的簡單web app,會用到Python中的streamlit和yfinance兩個庫。這個app理論上是要利用yfinance庫從雅虎財經(jīng)調(diào)取市場歷史數(shù)據(jù),然后把數(shù)據(jù)保存在數(shù)據(jù)框架中,最后作為streamlit 的輸入?yún)?shù)顯示出線形圖表。

寫出你的第一個web app,幾行代碼就夠了

圖源:unsplash

安裝必備庫

在這篇教程中,我們將用到兩個需要安裝的Python庫,streamlit 和yfinance。通過下面的pip install命令可以輕松地安裝streamlit:

pip install streamlit

同理按照以下命令完成 yfinance的安裝:

pip install yfinance

web app代碼

這次構(gòu)建web app的代碼一共不到20行,如果不算注釋的話代碼只有14行。

import yfinance as yf import streamlit as st
 
 st.write(""" # Simple StockPrice App Shown are the stockclosing price and volume of Google! """) # https://towardsdatascience.com/how-to-get-stock-data-using-python-c0de1df17e75 #define the tickersymbol tickerSymbol ='GOOGL' #get data on thisticker tickerData = yf.Ticker(tickerSymbol) #get the historicalprices for this ticker tickerDf = tickerData.history(period='1d', start='2010-5-31', end='2020-5-31') # Open HighLowClose Volume Dividends StockSplits st.line_chart(tickerDf.Close) st.line_chart(tickerDf.Volume)

代碼逐行解釋

現(xiàn)在,我們來詳細看看上面這些代碼:

· 第一行和第二行——輸入yfinance,命名為yf同時輸入streamlit,命名為st。

· 四至七行——用 st.write() 函數(shù)輸出文本內(nèi)容,輸出文本為markdown格式。

· 九至十六行——用yfinance庫從雅虎財經(jīng)中調(diào)取市場歷史數(shù)據(jù)。

· 十一行——定義股票代碼為 GOOGL。

· 十三行——用yf.Ticker()函數(shù)創(chuàng)建tickerData變量,顧名思義該變量為股票代碼數(shù)據(jù)。需要注意的是tickerData是一個股票代碼對象,如果把tickerData當作命令運行可以得到這樣的輸出 yfinance.Ticker object <GOOGL>。

· 十五行——創(chuàng)建tickerDf 數(shù)據(jù)框架,定義日期范圍(從2010年5月31日到2020年5月31日)以及時段(1天)。

寫出你的第一個web app,幾行代碼就夠了


· 十八行到十九行——用st.line_chart()函數(shù)畫出線性圖表(收盤價格數(shù)據(jù)來自十五行代碼中定義的tickerDf數(shù)據(jù)框架中的收盤與成交量數(shù)據(jù))。

運行web app

代碼保存在名為myapp.py的文件之后,啟動命令提示符(或者是微軟Windows系統(tǒng)里的Power Shell),運行下面的命令:

.py

然后可以看到以下信息:

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

不久一個網(wǎng)頁窗口就會彈出,直接跳去如下所示的http://localhost:8501這個創(chuàng)建好的web app。

寫出你的第一個web app,幾行代碼就夠了

股價web app截屏

Bingo!你已經(jīng)成功用Python創(chuàng)建了自己的第一個web app!

寫出你的第一個web app,幾行代碼就夠了

圖源:unsplash

自定義web app

以上是最基本的,如果你想讓自定義web app變得更有趣一點呢?

import yfinance as yf
 import streamlit as st
 
 st.write(""" # Simple StockPrice App Shown are the stock**closing price** and ***volume*** of Google! """)
 
 #https://towardsdatascience.com/how-to-get-stock-data-using-python-c0de1df17e75
 #define the tickersymbol
 tickerSymbol ='GOOGL' #get data on thisticker
 tickerData = yf.Ticker(tickerSymbol)
 #get the historicalprices for this ticker
 tickerDf = tickerData.history(period='1d', start='2010-5-31', end='2020-5-31')
 # Open HighLowClose Volume Dividends StockSplits
 
 st.write(""" ## Closing Price """)
 st.line_chart(tickerDf.Close)
 st.write(""" ## Volume """)
 st.line_chart(tickerDf.Volume)

讓我們花點時間來理解上面的代碼:

· 第六行——注意這里需在“closing price” 前后各用兩個星號來加粗“closing price” ,如下所示:**closingprice**。還要注意“volume”斜體加粗,前后各用三個星號,如下所示:***volume***。

· 十八至二十行和二十二至二十五行——在收盤價格和成交量圖表上方加入一個markdown格式的標題。

寫出你的第一個web app,幾行代碼就夠了

更新后的web app截屏

成功了!現(xiàn)在這個web app可以自動更新了。構(gòu)建你的第一個web app,就是這么簡單,快去上手操作一下吧!

寫出你的第一個web app,幾行代碼就夠了

本文標題:寫出你的第一個web app,幾行代碼就夠了
網(wǎng)址分享:http://www.muchs.cn/news10/100610.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、網(wǎng)站排名域名注冊、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司