plotly-express-3-Dash_callback

dash callbacks

The dash_html_components library provides classes for all of the HTML tags, and the keyword arguments describe the HTML attributes like style, className, and id.

The dash_core_components library generates higher-level components like controls and graphs.

  • dash_html_components提供的是HTML標(biāo)簽的各種類以及用于描述HTML屬性寄雀,比如style、className婿禽、id 等的各種關(guān)鍵參數(shù)
  • dash_core_components提供的是能夠用于控制和作圖的高級組件

第一個demo

代碼示例

在使用回調(diào)函數(shù)的時候需要引入一個模塊:

from dash.dependencies import Input, Output
image.png
image.png

代碼解釋
The “inputs” and “outputs” of our application interface are described declaratively through the app.callback decorator.

  • 應(yīng)用的中input和output接口通過回調(diào)裝飾器來實(shí)現(xiàn)

In Dash, the inputs and outputs of our application are simply the properties of a particular component. In this example, our input is the “value” property of the component that has the ID “my-id”. Our output is the “children” property of the component with the ID “my-div”.

  • inputs和outputs兩個部分只是特殊組件的屬性寺渗,它們同時擁有value和id兩個屬性值

Whenever an input property changes, the function that the callback decorator wraps will get called automatically. Dash provides the function with the new value of the input property as an input argument and Dash updates the property of the output component with whatever was returned by the function.

  • 只要輸入input的屬性改變,回調(diào)函數(shù)構(gòu)成的裝飾器會自動改變輸出值

The component_id and component_property keywords are optional (there are only two arguments for each of those objects). I have included them here for clarity but I will omit them from here on out for brevity and readability.

  • component_id and component_property這兩個參數(shù)可以省略疼进,直接寫出它們相應(yīng)的值即可

Notice how we don’t set a value for the children property of the my-div component in the layout. When the Dash app starts, it automatically calls all of the callbacks with the initial values of the input components in order to populate the initial state of the output components. In this example, if you specified something like html.Div(id='my-div', children='Hello world'), it would get overwritten when the app starts.

  • 上述例子中沒有對children屬性賦值薪缆。Dash應(yīng)用程序啟動時,它將自動使用輸入組件的初始值調(diào)用所有回調(diào)伞广,以填充輸出組件的初始狀態(tài)拣帽。如果將其設(shè)為其他值疼电,原始值將會被覆蓋。

第二個demo

另一個例子:通過dcc.Silder來更新dcc.Graph减拭,將滑動條和圖形相結(jié)合的例子

image.png
image.png

代碼解釋

  1. the "value" property of the Slider is the input of the app and the output of the app is the "figure" property of the Graph.
  • 指明了應(yīng)用APP的輸入slider和輸出figure
  1. Whenever the value of the Slider changes, Dash calls the callback function update_figure with the new value. The function filters the dataframe with this new value, constructs a figure object, and returns it to the Dash application.
  • 當(dāng)滑動條改變蔽豺,即輸入改變的時候,dash的回調(diào)函數(shù)也會同時更新拧粪,然后返回給dash應(yīng)用
  1. We load our dataframe at the start of the app: df = pd.read_csv('...'). This dataframe df is in the global state of the app and can be read inside the callback functions.
  • 在應(yīng)用APP開始的時候就導(dǎo)入了數(shù)據(jù)df修陡;保證了數(shù)據(jù)df是全局性質(zhì),在任何地方都可以讀取使用既们。
  1. The callback does not modify the original data, it just creates copies of the dataframe by filtering through pandas filters. This is important: your callbacks should never mutate variables outside of their scope.
  • 不要在回調(diào)函數(shù)內(nèi)部改變原始數(shù)據(jù)濒析,它僅僅是使用pandas來進(jìn)行過濾數(shù)據(jù),從而來使用其副本啥纸。
  1. We are turning on transitions with layout.transition to give an idea of how the dataset evolves with time: transitions allow the chart to update from one state to the next smoothly, as if it were animated.
  • 開啟了trasnsition屬性号杏,能夠看到數(shù)據(jù)集隨時間的變化情況。

可視化效果

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末斯棒,一起剝皮案震驚了整個濱河市盾致,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌荣暮,老刑警劉巖庭惜,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異穗酥,居然都是意外死亡护赊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門砾跃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來骏啰,“玉大人,你說我怎么就攤上這事抽高∨懈” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵翘骂,是天一觀的道長壁熄。 經(jīng)常有香客問我,道長碳竟,這世上最難降的妖魔是什么草丧? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮莹桅,結(jié)果婚禮上昌执,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好仙蚜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著厂汗,像睡著了一般委粉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上娶桦,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天贾节,我揣著相機(jī)與錄音,去河邊找鬼衷畦。 笑死栗涂,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的祈争。 我是一名探鬼主播斤程,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼菩混!你這毒婦竟也來了忿墅?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤沮峡,失蹤者是張志新(化名)和其女友劉穎疚脐,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邢疙,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡棍弄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了疟游。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呼畸。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖乡摹,靈堂內(nèi)的尸體忽然破棺而出役耕,到底是詐尸還是另有隱情,我是刑警寧澤聪廉,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布瞬痘,位于F島的核電站,受9級特大地震影響板熊,放射性物質(zhì)發(fā)生泄漏框全。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一干签、第九天 我趴在偏房一處隱蔽的房頂上張望津辩。 院中可真熱鬧,春花似錦、人聲如沸喘沿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蚜印。三九已至莺禁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間窄赋,已是汗流浹背哟冬。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留忆绰,地道東北人浩峡。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像错敢,于是被迫代替她去往敵國和親翰灾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354