1
+ '''
2
+ https://www.cnblogs.com/feffery/p/14687893.html
3
+ '''
4
+ import dash
5
+ #import dash_html_components as html
6
+ from dash import html
7
+ from dash import dcc
8
+ import dash_bootstrap_components as dbc
9
+
10
+ from dash .dependencies import Input , Output , State
11
+
12
+ import numpy as np
13
+
14
+ app = dash .Dash (__name__ )
15
+
16
+ app .layout = dbc .Container (
17
+ [
18
+ html .P (
19
+ [
20
+ html .Strong ('贵州茅台(600519)' ),
21
+ '最新股价:' ,
22
+ html .Span ('2108.94' , id = 'latest-price' )
23
+ ]
24
+ ),
25
+ dcc .Interval (id = 'demo-interval' , interval = 1000 )
26
+ ],
27
+ style = {
28
+ 'margin-top' : '100px'
29
+ }
30
+ )
31
+
32
+
33
+ @app .callback (
34
+ [Output ('latest-price' , 'children' ),
35
+ Output ('latest-price' , 'style' )],
36
+ Input ('demo-interval' , 'n_intervals' ),
37
+ State ('latest-price' , 'children' )
38
+ )
39
+ def fake_price_generator (n_intervals , latest_price ):
40
+ fake_price = float (latest_price ) + np .random .normal (0 , 0.1 )
41
+
42
+ if fake_price > float (latest_price ):
43
+ return f'{ fake_price :.2f} ' , {'color' : 'red' , 'background-color' : 'rgba(195, 8, 26, 0.2)' }
44
+
45
+ elif fake_price < float (latest_price ):
46
+ return f'{ fake_price :.2f} ' , {'color' : 'green' , 'background-color' : 'rgba(50, 115, 80, 0.2)' }
47
+
48
+ return f'{ fake_price :.2f} ' , {'background-color' : 'rgba(113, 120, 117, 0.2)' }
49
+
50
+
51
+ if __name__ == '__main__' :
52
+ app .run_server (port = 8000 , debug = True )
0 commit comments