Python Dash support (any ideas)

251 views
Skip to first unread message

RB

unread,
Jan 5, 2020, 4:19:17 AM1/5/20
to golden-cheetah-users
Hi,

I'm trying to get Dash (interactive graphs) working.

Small example:
REMINDER: you need to have install packages dash and flask
WARNING this code is working but will not stop until you hit the stop running button (use at onw risk best todo this with an test athlete).

import sys


import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input, Output
import os
from flask import request

external_stylesheets
= ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app
= dash.Dash(__name__, external_stylesheets=external_stylesheets)

df
= pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2016-weather-data-seattle.csv')
df
= df.dropna()
df
["Date"] = pd.to_datetime(df["Date"])
df
["year"] = df["Date"].dt.year

if 'DYNO' in os.environ:
    app_name
= os.environ['DASH_APP_NAME']
else:
    app_name
= 'dash-lineplot'

app
.layout = html.Div([html.Div([html.H1("Weather Records for Seattle")], style={'textAlign': "center"}),
                       html
.Div([html.Button('Stop Running', id='button'),
], id='quit-button'),
                       html
.Div([dcc.Dropdown(id="selected-value", multi=True, value=["Mean_TemperatureC"],
                                              options
=[{"label": "Maximum Temperature", "value": "Max_TemperatureC"},
                                                       
{"label": "Mean Temperature", "value": "Mean_TemperatureC"},
                                                       
{"label": "Minimum Temperature", "value": "Min_TemperatureC"}])],
                                className
="row", style={"display": "block", "width": "60%", "margin-left": "auto",
                                                       
"margin-right": "auto"}),
                       html
.Div([dcc.Graph(id="my-graph")]),
                       html
.Div([dcc.RangeSlider(id="year-range", min=1948, max=2015, step=1, value=[1998, 2000],
                                                 marks
={"1948": str(1948), "1954": str(1954), "1966": str(1966),
                                                       
"1975": str(1975), "1983": str(1983), "1994": str(1994),
                                                       
"2000": str(2000), "2005": str(2005), "2010": str(2010),
                                                       
"2012": str(2012), "2015": str(2015)})])
                       
], className="container")


@app.callback(
   
Output('my-graph', 'figure'),
   
[Input('selected-value', 'value'), Input('year-range', 'value'),
     
Input('button', 'n_clicks')])
def update_figure(selected, year, n_clicks):
   
if n_clicks == 1:
        shutdown_server
()
    text
= {"Max_TemperatureC": "Maximum Temperature", "Mean_TemperatureC": "Mean Temperature",
           
"Min_TemperatureC": "Minimum Temperature"}
    dff
= df[(df["year"] >= year[0]) & (df["year"] <= year[1])]
    trace
= []
   
for type in selected:
        trace
.append(go.Scatter(x=dff["Date"], y=dff[type], name=text[type], mode='lines',
                                marker
={'size': 8, "opacity": 0.6, "line": {'width': 0.5}}, ))


   
return {"data": trace,
           
"layout": go.Layout(title="Temperature Variations Over Time", colorway=['#fdae61', '#abd9e9', '#2c7bb6'],
                                yaxis
={"title": "Temperature ( degree celsius )"}, xaxis={"title": "Date"})}

def shutdown_server():
    func
= request.environ.get('werkzeug.server.shutdown')
   
if func is None:
       
raise RuntimeError('Not running with the Werkzeug Server')
    func
()


if __name__ == '__main__':
   
# Redirect pl
    GC
.webpage("http://127.0.0.1:8050/")
    app
.run_server(debug=False)


In this example i can explain the current problem.
Because the python code is keep running (app.run_server), GC is inresponsive.
So run in GC and open an external browser go to http://127.0.0.1:8050 and all is working fine.

Does anybody has an idea to fix this of have a workaround?

Kind regards,
Remco

Ale Martinez (Please don't email or cc me)

unread,
Jan 5, 2020, 6:56:23 AM1/5/20
to golden-cheetah-users
I haven’t tried this but a solution should be to run the server function in a background thread, see for example  https://realpython.com/intro-to-python-threading/

RB

unread,
Jan 5, 2020, 1:34:39 PM1/5/20
to golden-cheetah-users
Yes. it is working (threading is indeed the way to go).
Not sure yet when de thread is stopped also on reload with other threads (same ports like that) might need some more experiments but for now my first dash chart is running
see attachments.

Thx Ale,

Kind Regards,
Remco
Dash Test.gchart
Dash-test.png

Manuel Oberti

unread,
Jan 6, 2020, 12:08:55 PM1/6/20
to golden-cheetah-users
Good job Remco.....
Thanks

Oscar Martinez

unread,
Nov 21, 2021, 3:42:32 PM11/21/21
to golden-cheetah-users
Hi Ale,

How can install the library dash foy python on mac OSX, i need to execute correctly some graphs.

Many thanks!!

Ale Martinez

unread,
Nov 22, 2021, 6:32:33 AM11/22/21
to golden-cheetah-users
El domingo, 21 de noviembre de 2021 a la(s) 17:42:32 UTC-3, omba...@gmail.com escribió:
Hi Ale,

How can install the library dash foy python on mac OSX, i need to execute correctly some graphs.

They have a tutorial https://dash.plotly.com/installation 
Reply all
Reply to author
Forward
0 new messages