Skip to content

Commit 8504b0d

Browse files
committed
[update] Dash/test
1 parent e7b046c commit 8504b0d

13 files changed

+963
-0
lines changed

Dash/test/requirement_dash2.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Brotli==1.0.9
2+
click==8.1.3
3+
colorama==0.4.5
4+
contourpy==1.0.5
5+
cycler==0.11.0
6+
dash==2.6.2
7+
dash-bootstrap-components==1.2.1
8+
dash-core-components==2.0.0
9+
dash-datetimepicker==0.0.7
10+
dash-html-components==2.0.0
11+
dash-table==5.0.0
12+
Flask==2.2.2
13+
Flask-Compress==1.13
14+
Flask-SQLAlchemy==3.0.0
15+
fonttools==4.37.4
16+
greenlet==1.1.3
17+
importlib-metadata==5.0.0
18+
itsdangerous==2.1.2
19+
Jinja2==3.1.2
20+
kiwisolver==1.4.4
21+
MarkupSafe==2.1.1
22+
matplotlib==3.6.1
23+
numpy==1.23.3
24+
packaging==21.3
25+
pandas==1.5.0
26+
Pillow==9.2.0
27+
plotly==5.10.0
28+
pyparsing==3.0.9
29+
python-dateutil==2.8.2
30+
pytz==2022.4
31+
seaborn==0.12.0
32+
six==1.16.0
33+
SQLAlchemy==1.4.41
34+
tenacity==8.1.0
35+
tqdm==4.64.1
36+
Werkzeug==2.2.2
37+
zipp==3.9.0
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
import dash_datetimepicker
10+
from dash.dependencies import Input, Output, State
11+
import pandas as pd
12+
13+
app = dash.Dash(__name__)
14+
15+
app.layout = dbc.Container(
16+
[
17+
dash_datetimepicker.DashDatetimepicker(id="datetime-picker"),
18+
html.H6(id='datetime-output', style={'margin-top': '20px'})
19+
],
20+
style={
21+
'margin-top': '100px',
22+
'max-width': '600px'
23+
}
24+
)
25+
26+
27+
@app.callback(
28+
Output('datetime-output', 'children'),
29+
[Input('datetime-picker', 'startDate'),
30+
Input('datetime-picker', 'endDate')]
31+
)
32+
def datetime_range(startDate, endDate):
33+
# 修正8小时时间差bug并格式化为字符串
34+
startDate = (pd.to_datetime(startDate) + pd.Timedelta(hours=8)).strftime('%Y-%m-%d %H:%M')
35+
endDate = (pd.to_datetime(endDate) + pd.Timedelta(hours=8)).strftime('%Y-%m-%d %H:%M')
36+
37+
return f'从 {startDate}{endDate}'
38+
39+
if __name__ == '__main__':
40+
app.run_server(port=8000, debug=True)

Dash/test/test_dash_interval.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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)

Dash/test/test_dash_table_01.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
https://www.cnblogs.com/feffery/p/14616652.html
3+
'''
4+
import dash
5+
#import dash_html_components as html
6+
from dash import html
7+
import dash_bootstrap_components as dbc
8+
from dash import dash_table
9+
10+
import seaborn as sns
11+
12+
app = dash.Dash(__name__)
13+
14+
# 载入演示数据集
15+
df = sns.load_dataset('iris')
16+
# 创建行下标列
17+
df.insert(loc=0, column='#', value=df.index)
18+
19+
app.layout = html.Div(
20+
dbc.Container(
21+
dash_table.DataTable(
22+
columns=[{'name': column, 'id': column} for column in df.columns],
23+
data=df.to_dict('records'),
24+
virtualization=True
25+
),
26+
style={
27+
'margin-top': '100px'
28+
}
29+
)
30+
)
31+
32+
if __name__ == '__main__':
33+
app.run_server(port=8000, debug=True)

Dash/test/test_dash_table_04.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
https://www.cnblogs.com/feffery/p/14616652.html
3+
'''
4+
import dash
5+
#import dash_html_components as html
6+
from dash import html
7+
import dash_bootstrap_components as dbc
8+
from dash import dash_table
9+
10+
import seaborn as sns
11+
12+
app = dash.Dash(__name__)
13+
14+
# 载入演示数据集
15+
df = sns.load_dataset('iris')
16+
# 创建行下标列
17+
df.insert(loc=0, column='#', value=df.index)
18+
19+
app.layout = html.Div(
20+
dbc.Container(
21+
[
22+
dash_table.DataTable(
23+
columns=[{'name': column, 'id': column} for column in df.columns],
24+
data=df.to_dict('records'),
25+
virtualization=True,
26+
style_as_list_view=False,#True
27+
style_table={
28+
'height': '500px'
29+
},
30+
style_cell={
31+
'font-family': 'Times New Roman',
32+
'text-align': 'center'
33+
},
34+
style_header_conditional=[
35+
{
36+
'if': {
37+
# 选定列id为#的列
38+
'column_id': '#'
39+
},
40+
'font-weight': 'bold',
41+
'font-size': '24px'
42+
}
43+
],
44+
style_data_conditional=[
45+
{
46+
'if': {
47+
# 选中行下标为奇数的行
48+
'row_index': 'odd'
49+
},
50+
'background-color': '#cfd8dc'
51+
}
52+
]
53+
)
54+
],
55+
style={
56+
'margin-top': '100px'
57+
}
58+
)
59+
)
60+
61+
if __name__ == '__main__':
62+
app.run_server(port=8000, debug=True)

0 commit comments

Comments
 (0)