最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python開(kāi)發(fā)實(shí)時(shí)可視化儀表盤(pán)的示例

 更新時(shí)間:2021年05月07日 16:12:32   作者:費(fèi)弗里  
這篇文章主要介紹了python開(kāi)發(fā)實(shí)時(shí)可視化儀表盤(pán)的示例,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下

本文示例代碼已上傳至我的Github倉(cāng)庫(kù)https://github.com/CNFeffery/DataScienceStudyNotes

1 簡(jiǎn)介

這是我的系列教程「Python+Dash快速web應(yīng)用開(kāi)發(fā)」的第十五期,在前面的一系列教程中,我們針對(duì)Dash中的各種常用基礎(chǔ)概念作了比較詳細(xì)的介紹,如果前面的教程你有認(rèn)真學(xué)習(xí),那么相信到今天你已經(jīng)有能力開(kāi)發(fā)初具規(guī)模的Dash應(yīng)用了。

而在Dash生態(tài)中還有一系列功能比較特殊但又非常實(shí)用的部件,今天的文章我們就來(lái)學(xué)習(xí)這些常用的「特殊部件」。

2 Dash中的常用特殊功能部件

2.1 用Store()來(lái)存儲(chǔ)數(shù)據(jù)

在dash_core_components中有著很多功能特殊的部件,Store()就是其中之一,它的功能十分的簡(jiǎn)單,就是用來(lái)存儲(chǔ)數(shù)據(jù)的,譬如存儲(chǔ)一些數(shù)值、字符串等基礎(chǔ)數(shù)據(jù)類型或者把Python中的列表、字典等作為json格式數(shù)據(jù)存進(jìn)去。

Store()的主要參數(shù)/屬性除了id之外,還有:

data,代表其所存放的數(shù)據(jù),也是我們編寫(xiě)回調(diào)函數(shù)時(shí)關(guān)注的屬性;

modified_timestamp,用于記錄最后一次data屬性被修改的時(shí)間戳,通常用不到;

storage_type,用于設(shè)置存儲(chǔ)數(shù)據(jù)的生命周期,有3種,storage_type='memory'時(shí)生命周期最短,只要頁(yè)面一刷新,data就會(huì)恢復(fù)初始狀態(tài);storage_type='session'時(shí),只有瀏覽器被關(guān)閉時(shí)data才會(huì)被重置;而最后一種storage_type='local'時(shí),會(huì)將數(shù)據(jù)存儲(chǔ)在本地緩存中,只有手動(dòng)清除,data才會(huì)被重置。

話不多說(shuō),直接來(lái)看一個(gè)直觀的例子:

app1.py

import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = dbc.Container(
    [
        dbc.Form(
            [
                dbc.FormGroup(
                    [
                        dbc.Label('storage = "memory"時(shí)'),
                        dbc.Input(id='input-memory1', autoComplete='off'),
                        dbc.Input(id='input-memory2', style={'margin-top': '3px'}),
                        dcc.Store(id='data-in-memory')
                    ]
                ),
                dbc.FormGroup(
                    [
                        dbc.Label('storage = "session"時(shí)'),
                        dbc.Input(id='input-session1', autoComplete='off'),
                        dbc.Input(id='input-session2', style={'margin-top': '3px'}),
                        dcc.Store(id='data-in-session', storage_type='session')
                    ]
                ),
                dbc.FormGroup(
                    [
                        dbc.Label('storage = "local"時(shí)'),
                        dbc.Input(id='input-local1', autoComplete='off'),
                        dbc.Input(id='input-local2', style={'margin-top': '3px'}),
                        dcc.Store(id='data-in-local', storage_type='local')
                    ]
                ),
            ]
        )
    ],
    style={
        'margin-top': '100px',
        'max-width': '600px'
    }
)


# memory對(duì)應(yīng)回調(diào)
@app.callback(
    Output('data-in-memory', 'data'),
    Input('input-memory1', 'value')
)
def data_in_memory_save_data(value):
    if value:
        return value

    return dash.no_update


@app.callback(
    Output('input-memory2', 'placeholder'),
    Input('data-in-memory', 'data')
)
def data_in_memory_placeholder(data):
    if data:
        return data

    return dash.no_update


# session對(duì)應(yīng)回調(diào)
@app.callback(
    Output('data-in-session', 'data'),
    Input('input-session1', 'value')
)
def data_in_session_save_data(value):
    if value:
        return value

    return dash.no_update


@app.callback(
    Output('input-session2', 'placeholder'),
    Input('data-in-session', 'data')
)
def data_in_session_placeholder(data):
    if data:
        return data

    return dash.no_update


# local對(duì)應(yīng)回調(diào)
@app.callback(
    Output('data-in-local', 'data'),
    Input('input-local1', 'value')
)
def data_in_local_save_data(value):
    if value:
        return value

    return dash.no_update


@app.callback(
    Output('input-local2', 'placeholder'),
    Input('data-in-local', 'data')
)
def data_in_local_placeholder(data):
    if data:
        return data

    return dash.no_update


if __name__ == '__main__':
    app.run_server(debug=True)

可以看到,不同storage參數(shù)對(duì)應(yīng)的數(shù)據(jù),生命周期有著很大的區(qū)別:

就是憑借著這種自由存儲(chǔ)數(shù)據(jù)的特性,Store()可以幫助我們完成很多非常實(shí)用的功能,我們會(huì)在本文最后的例子里進(jìn)行展示。

2.2 用Interval()實(shí)現(xiàn)周期性回調(diào)

同樣是dash_core_components中的組件,Interval()的功能也很有意思,它可以幫助我們實(shí)現(xiàn)周期性自動(dòng)回調(diào),譬如開(kāi)發(fā)一個(gè)實(shí)時(shí)股價(jià)系統(tǒng),每隔一段時(shí)間就從后臺(tái)獲取最新的數(shù)據(jù),無(wú)需我們手動(dòng)刷新頁(yè)面,其主要的參數(shù)/屬性有:

n_intervals,Interval()的核心屬性,所謂的自動(dòng)更新實(shí)際上就是自動(dòng)對(duì)n_intervals的遞增過(guò)程;

interval,數(shù)值型,用于設(shè)置每隔多少毫秒對(duì)n_intervals的值進(jìn)行一次遞增,默認(rèn)為1000即1秒;

max_intervals,int型,用于設(shè)置在經(jīng)歷多少次遞增后,不再繼續(xù)自動(dòng)更新,默認(rèn)為-1即不限制;

disabled,bool型,默認(rèn)為False,用于設(shè)置是否停止遞增更新過(guò)程,如果說(shuō)max_intervals控制的過(guò)程是for循環(huán)的話,disabled就是while循環(huán),我們可以利用它自行編寫(xiě)邏輯在特定的條件下停止Interval()的遞增過(guò)程。

下面我們從一個(gè)偽造數(shù)據(jù)的股價(jià)實(shí)時(shí)更新系統(tǒng)例子中進(jìn)一步理解Interval()的作用:

app2.py

import dash
import numpy as np
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__)

app.layout = dbc.Container(
    [
        html.P(
            [
                html.Strong('貴州茅臺(tái)(600519)'),
                '最新股價(jià):',
                html.Span('2108.94', id='latest-price')
            ]
        ),
        dcc.Interval(id='demo-interval', interval=1000)
    ],
    style={
        'margin-top': '100px'
    }
)


@app.callback(
    [Output('latest-price', 'children'),
     Output('latest-price', 'style')],
    Input('demo-interval', 'n_intervals'),
    State('latest-price', 'children')
)
def fake_price_generator(n_intervals, latest_price):
    fake_price = float(latest_price) + np.random.normal(0, 0.1)

    if fake_price > float(latest_price):
        return f'{fake_price:.2f}', {'color': 'red', 'background-color': 'rgba(195, 8, 26, 0.2)'}

    elif fake_price < float(latest_price):
        return f'{fake_price:.2f}', {'color': 'green', 'background-color': 'rgba(50, 115, 80, 0.2)'}

    return f'{fake_price:.2f}', {'background-color': 'rgba(113, 120, 117, 0.2)'}


if __name__ == '__main__':
    app.run_server(debug=True)

哈哈,是不是非常的實(shí)用~

2.3 利用ColorPicker()進(jìn)行交互式色彩設(shè)置

接下來(lái)我們要介紹的這個(gè)很有意思的部件來(lái)自Dash的官方依賴dash_daq,它并不是自帶的,我們需要用pip進(jìn)行安裝。

ColorPicker()的功能是渲染出一個(gè)交互式的色彩選擇部件,使得我們可以更方便更直觀地選擇色彩值,其主要參數(shù)/屬性有:

label,字符串或字典,若只傳入字符串,則傳入的文字會(huì)作為渲染出的色彩選擇器的標(biāo)題,若傳入字典,其label鍵值對(duì)用于設(shè)置標(biāo)題文本內(nèi)容,style參數(shù)用于自定義css樣式;

labelPosition,字符型,top時(shí)標(biāo)題會(huì)置于頂部,bottom時(shí)會(huì)置于底部;

size,設(shè)置部件整體的像素寬度

value,字典型,作為參數(shù)時(shí)可以用來(lái)設(shè)定色彩選擇器的初始色彩,作為屬性時(shí)可以獲取當(dāng)前色彩選擇器的選定色彩,hex鍵值對(duì)可以直接獲取十六進(jìn)制色彩值,rgb鍵對(duì)應(yīng)的值為包含r、g、b和a四個(gè)鍵值對(duì)的字典,即構(gòu)成rgba色彩值的三通道+透明度值。

讓我們通過(guò)下面這個(gè)簡(jiǎn)單的例子來(lái)認(rèn)識(shí)它的工作過(guò)程:

app3.py

import dash
import dash_daq as daq
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = dbc.Container(
    [
        daq.ColorPicker(
            id='color-picker',
            label={
                'label': '色彩選擇器',
                'style': {
                    'font-size': '18px',
                    'font-family': 'SimHei',
                    'font-weight': 'bold'
                }
            },
            size=400,
            value=dict(hex="#120E03")
        ),
        html.P(
            '測(cè)試'*100,
            id='demo-p',
            style={
                'margin-top': '20px'
            }
        )
    ],
    style={
        'margin-top': '30px',
        'max-width': '500px'
    }
)

app.clientside_callback(
    """
    function(color) {
        return {'color': color.hex, 'margin-top': '20px'};
    }
    """,
    Output('demo-p', 'style'),
    Input('color-picker', 'value')
)

if __name__ == '__main__':
    app.run_server(debug=True)

動(dòng)圖錄制出來(lái)因?yàn)楸粔嚎s了所以色彩區(qū)域看起來(lái)跟打了碼似得:

實(shí)際上是這樣的:

2.4 利用DashDatetimepicker()進(jìn)行時(shí)間范圍選擇

接下來(lái)我要給大家介紹的這個(gè)部件DashDatetimepicker()也是來(lái)自第三方庫(kù),它基于react-datetime,可以幫助我們創(chuàng)建進(jìn)行日期選擇功能的部件(其實(shí)dash-core_components中也有類似功能的DatePickerRange()部件,但是太丑了,而且對(duì)中文支持的不好)。

使用pip install dash_datetimepicker完成安裝之后,默認(rèn)的部件月份和星期的名稱顯示都是英文的,我通過(guò)對(duì)相關(guān)的js源碼略加修改之后,便可以使用中文了,大家使用的時(shí)候把本期附件中的dash_datetimepicker.min.js放到assets目錄下即可。

DashDatetimepicker()使用起來(lái)非常簡(jiǎn)單,除了id之外,我們只需要在回調(diào)中獲取它的startDate與endDate屬性即可捕獲到用戶設(shè)置的日期時(shí)間范圍(在回調(diào)中我們接收到的開(kāi)始結(jié)束時(shí)間需要加上8個(gè)小時(shí),這是個(gè)bug):

app4.py

import dash
import pandas as pd
import dash_datetimepicker
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = dbc.Container(
    [
        dash_datetimepicker.DashDatetimepicker(id="datetime-picker"),
        html.H6(id='datetime-output', style={'margin-top': '20px'})
    ],
    style={
        'margin-top': '100px',
        'max-width': '600px'
    }
)


@app.callback(
    Output('datetime-output', 'children'),
    [Input('datetime-picker', 'startDate'),
     Input('datetime-picker', 'endDate')]
)
def datetime_range(startDate, endDate):
    # 修正8小時(shí)時(shí)間差bug并格式化為字符串
    startDate = (pd.to_datetime(startDate) + pd.Timedelta(hours=8)).strftime('%Y-%m-%d %H:%M')
    endDate = (pd.to_datetime(endDate) + pd.Timedelta(hours=8)).strftime('%Y-%m-%d %H:%M')

    return f'從 {startDate} 到 {endDate}'


if __name__ == "__main__":
    app.run_server(debug=True)

3 動(dòng)手打造一個(gè)實(shí)時(shí)可視化大屏

在學(xué)習(xí)完今天的內(nèi)容之后,我們就可以做一些功能上很amazing的事情——搭建一個(gè)實(shí)時(shí)更新的可視化儀表盤(pán)。

思路其實(shí)很簡(jiǎn)單,主要用到今天學(xué)習(xí)到的Interval()與Store(),原理是先從官網(wǎng)靜態(tài)的案例中移植js代碼到Dash的瀏覽器端回調(diào)中,構(gòu)建出輸入為Store()的data的回調(diào)函數(shù);

再利用Interval()的n_intervals觸發(fā)Store()的data更新,從而實(shí)現(xiàn)這套從數(shù)據(jù)更新到圖表更新的鏈?zhǔn)椒磻?yīng)。效果如下:

而代碼涉及到多個(gè)文件,這里就不直接放出,你可以在文章開(kāi)頭的地址中找到對(duì)應(yīng)本期的附件進(jìn)行學(xué)習(xí)。

以上就是python開(kāi)發(fā)實(shí)時(shí)可視化儀表盤(pán)的示例的詳細(xì)內(nèi)容,更多關(guān)于python開(kāi)發(fā)實(shí)時(shí)可視化儀表盤(pán)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python必學(xué)知識(shí)之裝飾器詳解

    python必學(xué)知識(shí)之裝飾器詳解

    這篇文章主要介紹了python必學(xué)知識(shí)之裝飾器詳解,python的三大器指的是:裝飾器、迭代器、生成器,下面就裝飾器整理一下從各種資源收獲的對(duì)裝飾器的理解,需要的朋友可以參考下
    2023-09-09
  • python解析庫(kù)Beautiful?Soup安裝的詳細(xì)步驟

    python解析庫(kù)Beautiful?Soup安裝的詳細(xì)步驟

    Beautiful?Soup是python的一個(gè)庫(kù),最主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于python解析庫(kù)Beautiful?Soup安裝的詳細(xì)步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Python IDLE或shell中切換路徑的操作

    Python IDLE或shell中切換路徑的操作

    這篇文章主要介紹了Python IDLE或shell中切換路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • PPOCRLabel標(biāo)注的txt格式如何轉(zhuǎn)換成labelme能修改的json格式

    PPOCRLabel標(biāo)注的txt格式如何轉(zhuǎn)換成labelme能修改的json格式

    這篇文章主要介紹了PPOCRLabel標(biāo)注的txt格式如何轉(zhuǎn)換成labelme能修改的json格式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python實(shí)現(xiàn)批量采集商品數(shù)據(jù)的示例詳解

    Python實(shí)現(xiàn)批量采集商品數(shù)據(jù)的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量采集商品的數(shù)據(jù),文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 利用pyinstaller或virtualenv將python程序打包詳解

    利用pyinstaller或virtualenv將python程序打包詳解

    這篇文章主要給大家介紹了利用pyinstaller將python程序打包的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • 使用python對(duì)多個(gè)txt文件中的數(shù)據(jù)進(jìn)行篩選的方法

    使用python對(duì)多個(gè)txt文件中的數(shù)據(jù)進(jìn)行篩選的方法

    今天小編就為大家分享一篇使用python對(duì)多個(gè)txt文件中的數(shù)據(jù)進(jìn)行篩選的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python實(shí)現(xiàn)的解析crontab配置文件代碼

    python實(shí)現(xiàn)的解析crontab配置文件代碼

    這篇文章主要介紹了python實(shí)現(xiàn)的解析crontab配置文件代碼,也可以說(shuō)是python版的crontab,代碼中包含大量注釋,需要的朋友可以參考下
    2014-06-06
  • python tkinter canvas 顯示圖片的示例

    python tkinter canvas 顯示圖片的示例

    今天小編就為大家分享一篇python tkinter canvas 顯示圖片的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • python使用技巧-標(biāo)準(zhǔn)輸入

    python使用技巧-標(biāo)準(zhǔn)輸入

    這篇文章主要介紹了python使用技巧標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸入即stdin ,下文圍繞python使用技巧標(biāo)準(zhǔn)輸入相關(guān)資料展開(kāi)學(xué)習(xí)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下
    2022-02-02

最新評(píng)論

无为县| 临泉县| 五原县| 马尔康县| 邯郸市| 会泽县| 万载县| 乐山市| 西藏| 娄底市| 明星| 筠连县| 通城县| 察雅县| 兴和县| 科技| 固安县| 芦溪县| 伊吾县| 盱眙县| 遂平县| 武平县| 射阳县| 海安县| 繁峙县| 大港区| 双峰县| 祁东县| 永修县| 博野县| 犍为县| 郑州市| 合作市| 固阳县| 天柱县| 岳普湖县| 阿城市| 大埔区| 沽源县| 芜湖市| 西昌市|