Skip to content

Instantly share code, notes, and snippets.

View davidcomfort's full-sized avatar

David Michael Comfort davidcomfort

View GitHub Profile
# First Data Table Update Function
def update_first_datatable(start_date, end_date, category, aggregation):
if start_date is not None:
start_date = dt.strptime(start_date, '%Y-%m-%d')
start_date_string = start_date.strftime('%Y-%m-%d')
if end_date is not None:
end_date = dt.strptime(end_date, '%Y-%m-%d')
end_date_string = end_date.strftime('%Y-%m-%d')
days_selected = (end_date - start_date).days
# Define Formatters
def formatter_currency(x):
return "${:,.0f}".format(x) if x >= 0 else "(${:,.0f})".format(abs(x))
def formatter_currency_with_cents(x):
return "${:,.2f}".format(x) if x >= 0 else "(${:,.2f})".format(abs(x))
def formatter_percent(x):
return "{:,.1f}%".format(x) if x >= 0 else "({:,.1f}%)".format(abs(x))
# Callback and update first data table
@app.callback(Output('datatable-paid-search', 'data'),
[Input('my-date-picker-range-paid-search', 'start_date'),
Input('my-date-picker-range-paid-search', 'end_date')])
def update_data_1(start_date, end_date):
data_1 = update_first_datatable(start_date, end_date, 'Paid Search', 'Placement type')
return data_1
# First Data Table
html.Div([
dash_table.DataTable(
id='datatable-paid-search',
columns=[{"name": i, "id": i, 'deletable': True} for i in dt_columns],
editable=True,
n_fixed_columns=2,
style_table={'maxWidth': '1500px'},
row_selectable="multi",
selected_rows=[0],
# Callback and update data table columns
@app.callback(Output('datatable-paid-search', 'columns'),
[Input('radio-button-paid-search', 'value')])
def update_columns(value):
if value == 'Complete':
column_set=[{"name": i, "id": i, 'deletable': True} for i in columns_complete] + [{"name": j, "id": j, 'hidden': 'True'} for j in conditional_columns]
elif value == 'Condensed':
column_set=[{"name": i, "id": i, "deletable": True} for i in columns_condensed]
return column_set
# Callback and update data table columns
@app.callback(Output('datatable-paid-search', 'columns'),
[Input('radio-button-paid-search', 'value')])
def update_columns(value):
if value == 'Complete':
column_set=[{"name": i, "id": i, 'deletable': True} for i in columns_complete] + [{"name": j, "id": j, 'hidden': 'True'} for j in conditional_columns]
elif value == 'Condensed':
column_set=[{"name": i, "id": i, "deletable": True} for i in columns_condensed]
return column_set
# Radio Button
html.Div([
dcc.RadioItems(
options=[
{'label': 'Condensed Data Table', 'value': 'Condensed'},
{'label': 'Complete Data Table', 'value': 'Complete'},
], value='Condensed',
labelStyle={'display': 'inline-block', 'width': '20%', 'margin':'auto', 'marginTop': 15, 'paddingLeft': 15},
id='radio-button-paid-search'
)]),
#datatable-paid-search > div > div > div.row.row-1 > div.cell.cell-1-0.dash-fixed-column {
flex: 0 0 auto;
left: 0;
position: sticky;
z-index: 0;
}
#### Date Picker Callback
@app.callback(Output('output-container-date-picker-range-paid-search', 'children'),
[Input('my-date-picker-range-paid-search', 'start_date'),
Input('my-date-picker-range-paid-search', 'end_date')])
def update_output(start_date, end_date):
string_prefix = 'You have selected '
if start_date is not None:
start_date = dt.strptime(start_date, '%Y-%m-%d')
start_date_string = start_date.strftime('%B %d, %Y')
string_prefix = string_prefix + 'a Start Date of ' + start_date_string + ' | '
# Date Picker
html.Div([
dcc.DatePickerRange(
id='my-date-picker-range-paid-search',
min_date_allowed=dt(2018, 1, 1),
max_date_allowed=df['Date'].max().to_pydatetime(),
initial_visible_month=dt(current_year,df['Date'].max().to_pydatetime().month, 1),
start_date=(df['Date'].max() - timedelta(6)).to_pydatetime(),
end_date=df['Date'].max().to_pydatetime(),
),