-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
100 lines (85 loc) · 2.65 KB
/
example.py
File metadata and controls
100 lines (85 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import dash
from dash import dcc, html
import dash_bootstrap_components as dbc
import pandas as pd
import datetime
import dash_dataframe_table
import os
parent_dir = os.getcwd().split('/')[-1]
with open("example.md", "r") as myfile:
markdown_text = myfile.read()
app = dash.Dash("Example of Enhanced Table from dataframe",
external_stylesheets=[dbc.themes.YETI],
title="Example Data Frame",
meta_tags=[
{
"name": "viewport",
"content": "width=device-width, initial-scale=1"
},
],
url_base_pathname=f'/dash/{parent_dir}/')
server = app.server
the_list = [
"Apple", "Google", "Yahoo", "Facebook", "Microsoft", "Amazon", "IBM",
"Intel", "Oracle"
]
start_date = datetime.datetime(2018, 1, 1)
date_list = pd.date_range(start_date, periods=len(the_list)).tolist()
## make a simple dataframe with links
df = pd.DataFrame([{
'Company':
x,
"Company_HREF":
f"https://{x.lower()}.com",
"Value": (n - 4) / 13,
"Value2": (n**4) / 13,
"Date":
date_list[n],
"markdown_example":
f"""Everything in **here** is plain text in _Markdown_, created with an fstring for Company **{x}** """
} for n, x in enumerate(the_list)])
def color_positive(val):
if val > 0:
return {'className': 'table-success'}
elif val < 0:
return {'className': "table-warning"}
## make the conditional styling dictionary
cell_style_dict = {
'Company': [
(['Yahoo', 'Apple'], {
'font-weight': 'bold'
}),
(['Oracle'], {
'className': 'table-danger'
}),
],
'Value2':
lambda x: {
'background-color': '#7FFFD4'
} if x > 10 else {
}, ## these needed because the callable gets applied on the string header.
## maybe the header should have its own callable tha controls class
'Date':
lambda x: {
'className': 'table-info'
} if x.weekday() in [4, 6] else {},
'Value':
color_positive
}
col_one = dbc.Col(dcc.Markdown(markdown_text), )
col_two = dbc.Col([
html.H4('Rendered Table from Dataframe'),
dbc.Table.from_enhanced_dataframe(
df,
striped=True,
cell_style_dict=cell_style_dict,
id='hello',
float_format='.2f',
date_format='%Y-%m-%d',
columns=['Company', 'Date', 'Value', 'Value2', 'markdown_example'],
markdown_columns=['markdown_example']),
])
app.layout = dbc.Container([dbc.Row([col_one, col_two])],
style={'margin-top': '10px'})
if __name__ == "__main__":
app.run_server(debug=True)