Hello,
I’m trying to use Dash app to visualize a graph if a condition is met in another python file. I’d like to avoid running the file twice which happens when I set debug=True
in app.run_server
method (solution here) But when I set it to False, I’m getting "GET /_reload-hash HTTP/1.1" 200 -
every 3 seconds. I’m not sure if it’s connected, but in documentation I’ve found a line
- dev_tools_hot_reload_interval`, float, interval in seconds at which the renderer will request the reload hash and update the browser page if it changed. (default=3).
But I don’t think that page changes. And even so, it’s defaulted to False if debug=False
.
My code:
import copy
from dash import Dash, dcc, html, Input, Output
import dash_cytoscape as cyto
import networkx as nx
from for_dash import get_edges
app = Dash(__name__)
edges, edge_labels, b = get_edges()
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
pos_param = 2000
nodes = [
{
"data": {"id": node, "label": node},
"position": {"x": pos_param * p[0], "y": pos_param * p[1]},
"classes": "green",
}
for node, p in pos.items()
]
for n in nodes:
if n["data"]["id"] in b:
n["classes"] = "red"
edges = [
{"data": {"source": edge[0], "target": edge[1], "label": label}}
for edge, label in edge_labels.items()
]
my_stylesheet = [
# Group selectors
{"selector": "node", "style": {"content": "data(label)"}},
# Class selectors
{"selector": ".red", "style": {"background-color": "red", "line-color": "red"}},
{
"selector": ".green",
"style": {"background-color": "green", "line-color": "green"},
},
]
app.layout = html.Div(
[
cyto.Cytoscape(
id="graph",
elements=nodes + edges,
stylesheet=my_stylesheet,
style={"width": "100%", "height": "600px"},
layout={"name": "preset"},
),
dcc.Checklist(
id="connections",
options=[
"Adres",
"Telefon",
"Email",
"Prepaid",
"Reverse prepaid",
"Same prepaid",
"Przelewy",
"GRYF",
"GIIF",
],
value=[
"Adres",
"Telefon",
"Email",
"Prepaid",
"Reverse prepaid",
"Same prepaid",
"Przelewy",
"GRYF",
"GIIF",
],
),
# dcc.RadioItems(
# id='radio-update-layout',
# value='preset',
# options=[
# {'label': name.capitalize(), 'value': name}
# for name in ['grid', 'random', 'circle', 'cose', 'concentric']
# ]
# ),
html.Br(),
dcc.Slider(
0.1,
5,
id="pos_param",
value=1,
marks={0.1: "0.1", 0.5: "0.5", 1: "1", 1.5: "1.5", 2: "2", 5: "5"},
),
]
)
@app.callback(
Output("graph", "elements"),
Input("connections", "value"),
Input("pos_param", "value"),
)
def update_edges(conn, p):
updated_edges = copy.deepcopy(edges)
updated_nodes = copy.deepcopy(nodes)
updated_edges = [
edge for edge in updated_edges if any(v in edge["data"]["label"] for v in conn)
]
updated_nodes = [
node
for node in updated_nodes
if any(node["data"]["id"] in e["data"].values() for e in updated_edges)
]
for node in updated_nodes:
node["position"]["x"] *= p
node["position"]["y"] *= p
return updated_nodes + updated_edges
if __name__ == "__main__":
app.run_server(debug=True, port=1234)
@edit
I’ve changed the code a bit to run the app from the other python file and this time I was getting refreshes every minute, for 4 minutes. Then I waited 2 minutes more but the refresh didn’t come up, so I’ve changed a slider value manually and now I’m getting refreshes every 3 seconds again.
@edit2
I’ve ran the scripts a couple times more and the problem doesn’t seem to occur again. I’d very much like to know why that happened, but I guess I’ll delete this post tomorrow if everything runs smoothly from now on.