Hi! Many thanks!
It work smoothly
my āclientsideā and JS knowledge are indeed quite limited, this was helpful!
I did a small variation which get the user_agent string and check if the device is touch or not
below the code if could be any useful
P.S.
"displayModeBar": True
it is not enough to make the plotly graph static, one should specify also
'staticPlot': False
Here is the small variation that check if device is touch
from dash import Dash, dcc, html, Input, Output
import plotly.express as px
import dash_bootstrap_components as dbc
df = px.data.tips()
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div([
dbc.Container(
[dcc.Store(id="store"), dbc.Card(html.Div(id="graph-container"))], fluid=True,),
])
app.clientside_callback(
"""
function(trigger) {
// can use any prop to trigger this callback - we just want to store the info on startup
// USE THIS TO GET screen dimensions
// const screenInfo = {height :screen.height, width: screen.width};
// USE THIS TO GET useragent string
screenInfo = navigator.userAgent;
return screenInfo
}
""",
Output("store", "data"),
Input("store", "data"),
)
@app.callback(Output("graph-container", "children"), Input("store", "data"))
def update(JSoutput):
############
# Check if it is "touch_capable"
############
print('THIS IS USER AGENT STRING:',JSoutput)
from user_agents import parse
user_agent = parse(JSoutput)
# if user_agent.is_mobile is True:
# if user_agent.is_tablet is True:
if user_agent.is_touch_capable is True:
print('####### DEVICE is touch')
return dcc.Graph(
id="graph",
figure=px.bar(df, x="day", y="total_bill"),
config={"displayModeBar": False,
'staticPlot': True},
)
else:
print('####### DEVICE is NOT touch')
return dcc.Graph(
id="graph",
figure=px.bar(df, x="day", y="total_bill"),
config={"displayModeBar": True,
'staticPlot': False},
)
############
# check screen width
############
# print(JSoutput["width"])
# if JSoutput and JSoutput["width"] < 500:
# return dcc.Graph(
# id="graph",
# figure=px.bar(df, x="day", y="total_bill"),
# config={"displayModeBar": False,
# 'staticPlot': True},
# )
# else:
# return dcc.Graph(
# id="graph",
# figure=px.bar(df, x="day", y="total_bill"),
# config={"displayModeBar": True,
# 'staticPlot': False},
# )
if __name__ == "__main__":
app.run_server(debug=True)