Last active
September 28, 2020 15:34
-
-
Save codepatel/c10f3db49166be30858f87e88be5bfb4 to your computer and use it in GitHub Desktop.
Sharing and collaborating on useful Dashboard apps with the URL of a web application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.callback([Output('ticker-input', 'value'), | |
Output('analysis-mode', 'value'), | |
Output('snapshot-uuid', 'value'), | |
Output('handler-parseURL', 'data')], | |
[Input('nav-dcf', 'active'), | |
Input('url', 'pathname')]) | |
def parse_ticker(dcf_app_active, pathname): | |
if dcf_app_active: | |
parse_ticker = pathname.split('/apps/dcf')[-1].split('/') | |
if len(parse_ticker) == 1: | |
return DEFAULT_TICKER, [1], str(uuid.uuid5(uuid.uuid4(), DEFAULT_TICKER)), dash.no_update | |
elif len(parse_ticker) == 2: | |
ticker_value = parse_ticker[1].upper() or DEFAULT_TICKER | |
return ticker_value, [1], str(uuid.uuid5(uuid.uuid4(), ticker_value)), dash.no_update | |
else: # >=3 | |
if parse_ticker[2]: | |
try: | |
uuid_val = uuid.UUID(parse_ticker[2], version=5) | |
if uuid_val.hex == parse_ticker[2].replace('-',''): | |
return parse_ticker[1].upper() or DEFAULT_TICKER, [], parse_ticker[2], dash.no_update | |
else: | |
raise ValueError("Bad Snapshot ID from URL: " + parse_ticker[2]) | |
except: | |
return parse_ticker[1].upper() or DEFAULT_TICKER, [], '', handler_data_message('See Error Message(s) below:', traceback.format_exc()) | |
else: | |
return parse_ticker[1].upper(), [], str(uuid.uuid5(uuid.uuid4(), parse_ticker[1].upper())), dash.no_update | |
else: | |
raise PreventUpdate | |
@app.callback([Output('snapshot-link', 'href'), | |
Output('save-snapshot', 'disabled'), | |
Output('snapshot-link', 'disabled')], | |
[Input('analysis-mode', 'value'), | |
Input('save-snapshot', 'n_clicks'), | |
Input('ticker-input', 'value'), | |
Input('snapshot-uuid', 'value')], | |
State('dcf-store', 'data')) | |
def save_snapshot(live_analysis_mode, save_button_clicked, ticker, snapshot_uuid, df_dict): | |
if 1 in live_analysis_mode: # generate a fresh UUID | |
snapshot_uuid = str(uuid.uuid5(uuid.UUID(snapshot_uuid), ticker)) | |
if save_button_clicked: | |
db.set(ticker+'-'+snapshot_uuid, json.dumps(df_dict)) | |
return '/apps/dcf/' + ticker + '/' + snapshot_uuid, False, not save_button_clicked | |
else: | |
return dash.no_update, True, True | |
@app.callback([Output('dcf-store', 'data')], | |
[Input('ticker-input', 'valid')], | |
[State('ticker-input', 'value'), | |
State('analysis-mode', 'value'), | |
State('snapshot-uuid', 'value')]) | |
def update_data(ticker_valid, ticker, live_analysis_mode, snapshot_uuid): | |
if not ticker_valid: | |
raise ValueError('Invalid Ticker: ' + ticker) | |
ticker_allcaps = ticker.upper() | |
db_key = ticker_allcaps+'-'+snapshot_uuid | |
if 1 in live_analysis_mode or not db.exists(db_key): | |
# get fresh data from source to update df_dict | |
else: | |
df_dict = json.loads(db.get(db_key)) # pull output callback from from server cache or database: redis | |
# Continue processing layout UI element updates with df_dict | |
return df_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment