Index.py
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash import callback_context
# Connect to main app.py file
from app import app
#from app import server
from apps import cyto_compound
from apps import backend_dataset
from apps import additional_filters
app.layout = html.Div(
[
#storage_type='session',
dcc.Store(id='store_cyto_compound'),
dbc.Row(
#for the moment, we put all in one column
#but maybe later put in separate columns
#just put one of each link into a different column
dbc.Col(
html.Div(
children=[
dcc.Location(id='url',pathname='',refresh=False),
dcc.Link('Compounds',href='/apps/cyto_compound'),
dcc.Link('Backend Dataset',href='/apps/backend_dataset'),
dcc.Link('Additional Filters',href='/apps/additional_filters')
]
),
)
),
dbc.Row(
dbc.Col(
html.Div(
id='page_content',
children=[]
)
)
)
]
)
@app.callback(
[Output(component_id='page_content',component_property='children')],
[Input(component_id='url',component_property='pathname')]
)
def display_page(temp_pathname):
if temp_pathname == '/apps/cyto_compound':
return [cyto_compound.layout]
elif temp_pathname == '/apps/backend_dataset':
return [backend_dataset.layout]
elif temp_pathname == '/apps/additional_filters':
return [additional_filters.layout]
else:
return 'under construction'
if __name__ == '__main__':
app.run_server(debug=True)
cyto_compound.py layout
layout=html.Div(
children=[
dbc.Row(
dbc.Col(
children=[
html.Button('add compound cyto', id='button_add_cyto_compound', n_clicks=0),
],
width='auto',
align='center'
)
),
html.Div(
id='div_cytoscape_compound_cyto',
children=[]
),
html.Div(
children=[
dbc.Row(
dbc.Col(
children=[
html.Button('bs button', id='bs button', n_clicks=0),
],
width='auto',
align='center'
)
),
]
),
]
)
Callbacks
@app.callback(
[Output(component_id='div_cytoscape_compound_cyto',component_property='children')],
#gets n_clicks=0 when app loads, thats why you get a cyto right off the bat
[Input(component_id='button_add_cyto_compound',component_property='n_clicks')],
[State(component_id='div_cytoscape_compound_cyto',component_property='children'),
State(component_id='store_cyto_compound',component_property='data')],prevent_initial_callback=True
)
def add_cyto_compound(temp_n_clicks,temp_children,temp_store):
if (callback_context.triggered[0]['prop_id']=='.'):
for i,element in enumerate(temp_store):
new_graph=dbc.Row(
dbc.Col(
dbc.Card(
children=[
#compounds
cyto.Cytoscape(
id={
'type':'cytoscape_compound',
'key':i
},
layout={'name':'dagre'},
elements=compound_network_dict['elements'],
stylesheet=stylesheet,
minZoom=0.3,
maxZoom=5
)
]
),
width='auto',
align='center'
)
)
temp_children.append(new_graph)
#if (callback_context.triggered[0]['prop_id']=='.'):
elif (callback_context.triggered[0]['prop_id']=='button_add_cyto_compound.n_clicks'):
temp_children.append(new_graph)
return [temp_children]
@app.callback(
[Output(component_id={'type':'cytoscape_compound','key':MATCH},component_property='elements')],
[Input(component_id={'type':'cytoscape_compound','key':MATCH},component_property='tapNodeData')],
#Input(component_id='button_add_cyto_compound',component_property='n_clicks')],
#Input(component_id='Compounds',component_property='href')],
[State(component_id={'type':'cytoscape_compound','key':MATCH},component_property='elements'),
State(component_id='store_cyto_compound',component_property='data')]#,prevent_initial_call=True
)
def update_node_selection(temp_tap,temp_elements,temp_store):
if temp_tap is None:
raise PreventUpdate
elif callback_context.triggered[0]['prop_id']=='.':
raise PreventUpdate
try:
child_nodes_and_self=nx.algorithms.dag.descendants(networkx,temp_tap['id'])
except nx.NetworkXError:
child_nodes_and_self=set()
child_nodes_and_self.add(temp_tap['id'])
child_nodes_and_self=set(map(str,child_nodes_and_self))
for temp_node in temp_elements['nodes']:
if temp_node['data']['id'] in child_nodes_and_self:
if temp_node['classes']=='selected':
temp_node['classes']='not_selected'
elif temp_node['classes']=='not_selected':
temp_node['classes']='selected'
return [temp_elements]
def check_if_selected(temp_dict):
if temp_dict['classes']=='selected':
return str(temp_dict['data']['id'])
@app.callback(
[Output(component_id='store_cyto_compound',component_property='data')],
[Input(component_id={'type':'cytoscape_compound','key':ALL},component_property='elements')],
[State(component_id='store_cyto_compound',component_property='data')]
,prevent_initial_call=True
)
def add_selections_to_store(temp_elements,temp_store):
print('\nadd_selections_to_store')
print(callback_context.triggered[0]['prop_id'])
#print(temp_elements)
if callback_context.triggered[0]['prop_id']=='.':
raise PreventUpdate
selected_ids_list=[list(map(check_if_selected,temp_cyto_dict['nodes'])) for temp_cyto_dict in temp_elements]
return [selected_ids_list]