1
1
from datetime import datetime
2
+ from pathlib import Path
2
3
4
+ import dash_bootstrap_components as dbc
3
5
import dash_html_components as html
4
6
import dash_table
5
7
import numpy as np
@@ -39,12 +41,12 @@ def make_dates_list(years=None, months=np.arange(1, 13, 1), days=np.arange(1, 32
39
41
40
42
41
43
def add_table (
42
- table_name = None ,
43
- df = None ,
44
- dropdown_options = None ,
45
- dropdown_columns = [] ,
46
- table_expandable = True ,
47
- table_width = 90 ,
44
+ table_name : str = None ,
45
+ df : pd . DataFrame = None ,
46
+ dropdown_options : dict = None ,
47
+ dropdown_columns : list = None ,
48
+ table_expandable : bool = True ,
49
+ table_width : int = 90 ,
48
50
** kwargs ,
49
51
):
50
52
"""Dash DataTable wrapper to provide dropdown columns along with free field columns. Tables are row extendable.
@@ -87,6 +89,7 @@ def add_table(
87
89
]
88
90
89
91
table_title = str (table_name ).replace ("_" , " " ).capitalize ()
92
+ table_button_name = "--" .join ([__name__ , table_name ]).replace ("." , "--" )
90
93
91
94
layout = html .Div (
92
95
[
@@ -102,26 +105,29 @@ def add_table(
102
105
dropdown = dropdown_options ,
103
106
),
104
107
html .Div (id = f"{ table_name } -container" ),
105
- html .Button ("Add Row" , id = f" { table_name } -button" , n_clicks = 0 )
108
+ html .Button ("Add Row" , id = table_button_name , n_clicks = 0 )
106
109
if table_expandable
107
- else html .Div (),
108
- html .Br (),
110
+ else html .Div (hidden = True ),
109
111
],
110
112
style = {
111
113
"padding" : "10px" ,
112
114
"width" : "90%" if table_width is None else f"{ table_width } %" ,
113
115
},
114
116
)
115
117
116
- @callback (
117
- Output (table_name , "data" ),
118
- Input (f"{ table_name } -button" , "n_clicks" ),
119
- State (table_name , "data" ),
120
- State (table_name , "columns" ),
121
- )
122
- def add_row (n_clicks , rows , columns ):
123
- if n_clicks > 0 :
124
- rows .append ({c ["id" ]: "" for c in columns })
125
- return rows
118
+ if table_expandable :
119
+
120
+ @callback (
121
+ Output (table_name , "data" ),
122
+ [
123
+ Input (table_button_name , "n_clicks" ),
124
+ State (table_name , "data" ),
125
+ State (table_name , "columns" ),
126
+ ],
127
+ )
128
+ def add_row (n_clicks , rows , columns ):
129
+ if n_clicks > 0 :
130
+ rows .append ({c ["id" ]: "" for c in columns })
131
+ return rows
126
132
127
133
return layout
0 commit comments