Skip to content

Commit

Permalink
Remove display of graphs of word is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
pbong committed Oct 31, 2023
1 parent a5116aa commit ccf248c
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 81 deletions.
7 changes: 6 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
dcc.Store(
id='submitted-word',
storage_type='session'
),

dcc.Store(
id='word-exists',
storage_type='session'
)

], fluid=True)
Expand All @@ -69,7 +74,7 @@
callbacks.home.export.callbacks.init_callback(app, API_URL)

callbacks.home.scroll_callbacks.init_callback(app)
callbacks.home.home_callbacks.init_callback(app)
callbacks.home.home_callbacks.init_callback(app, API_URL)

if __name__ == '__main__':
app.run_server(port='8049', debug=True)
7 changes: 4 additions & 3 deletions src/callbacks/home/embeddings/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
def init_callback(app, API_URL):
@app.callback(
Output('embeddings', 'figure'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def display_embeddings(word):
if word:
def display_embeddings(word, word_exists):
if word and word_exists:
df = get_word_embeddings_db(API_URL, word)
sense_id_list = []

Expand Down
6 changes: 3 additions & 3 deletions src/callbacks/home/export/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dash import Input, Output, dcc
from dash import Input, Output, State, dcc
from plotly.graph_objs import *
from dash.exceptions import PreventUpdate
from ..api_query import *
Expand All @@ -8,8 +8,8 @@
def init_callback(app, API_URL):
@app.callback(
Output('download-embeddings', 'data'),
Input('submitted-word', 'data'),
Input('export-embeddings', 'n_clicks')
State('submitted-word', 'data'),
Input('export-embeddings', 'n_clicks'),
)
def download_lift_over_table_to_csv(word, export_embeddings):
if export_embeddings >= 1:
Expand Down
18 changes: 16 additions & 2 deletions src/callbacks/home/home_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
from dash import Input, Output, State
from dash.exceptions import PreventUpdate
from .api_query import *

def init_callback(app):
def init_callback(app, API_URL):
@app.callback(
Output('home-body-container', 'hidden'),
Output('home-sidebar', 'hidden'),
Output('submitted-word', 'data'),
Output('search-word-error', 'children'),
Output('search-word-error', 'hidden'),
Output('word-exists', 'data'),
Input('search-word-submit-btn', 'n_clicks'),
Input('search-word', 'n_submit'),
State('search-word', 'value')
)
def submit_input(n_clicks, n_submit, word):
if n_clicks >= 1 or n_submit >= 1:
return False, False, word.lower()
if word:
df = get_word_db(API_URL, word)

if len(df) >= 1:
return False, False, word.lower(), '', True, True

else:
return True, True, word.lower(), [f'No Word Found: {word}'], False, False

raise PreventUpdate

Expand Down
44 changes: 25 additions & 19 deletions src/callbacks/home/network/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ def init_callback(app, API_URL):
@app.callback(
Output('communities-dropdown', 'options'),
Output('communities-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def populate_communities_dropdown(word):
if word:
def populate_communities_dropdown(word, word_exists):
if word and word_exists:
return [{'label': 'Sense ' + str(i + 1), 'value': i} for i in range(get_num_senses(API_URL, word))], 0

raise PreventUpdate

@app.callback(
Output('input-word-network', 'children'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def search_word(word):
if word:
def search_word(word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)

if len(df) >= 1:
Expand All @@ -35,17 +37,21 @@ def search_word(word):
@app.callback(
Output('network-cooccurring-words', 'children'),
Input('submitted-word', 'data'),
Input('communities-dropdown', 'value')
Input('communities-dropdown', 'value'),
Input('word-exists', 'data')
)
def display_co_occurring_words(word, sense_id):
for entry in get_netsci_word(API_URL, word):
if entry['sense_id'] == f'ns_{word}_{sense_id}':
cooccurring_words = [html.Span(cooccurring_word, className='link-primary', style={'text-decoration': 'none'})
for cooccurring_word in entry['community']]

ret_val = []
for cooccurring_word in cooccurring_words:
ret_val.append(cooccurring_word)
ret_val.append(html.Span(' ', className='me-2'))

return ret_val
def display_co_occurring_words(word, sense_id, word_exists):
if word_exists:
for entry in get_netsci_word(API_URL, word):
if entry['sense_id'] == f'ns_{word}_{sense_id}':
cooccurring_words = [html.Span(cooccurring_word, className='link-primary', style={'text-decoration': 'none'})
for cooccurring_word in entry['community']]

ret_val = []
for cooccurring_word in cooccurring_words:
ret_val.append(cooccurring_word)
ret_val.append(html.Span(' ', className='me-2'))

return ret_val

raise PreventUpdate
49 changes: 28 additions & 21 deletions src/callbacks/home/plot/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
def init_callback(app, API_URL):
@app.callback(
Output('word-plot-sense', 'children'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def display_word_in_the_sense_plot_description(word):
if word:
def display_word_in_the_sense_plot_description(word, word_exists):
if word and word_exists:
return [
f' {word} ',
html.I(className='bi bi-info-circle'),
Expand All @@ -26,10 +27,11 @@ def display_word_in_the_sense_plot_description(word):

@app.callback(
Output('word-plot-source', 'children'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def display_word_in_the_source_plot_description(word):
if word:
def display_word_in_the_source_plot_description(word, word_exists):
if word and word_exists:
return [
f' {word} ',
html.I(className='bi bi-info-circle'),
Expand Down Expand Up @@ -98,10 +100,11 @@ def display_word_tooltip_in_the_source_plot(source_n_clicks, sense_n_clicks, wor
@app.callback(
Output('sense-dropdown', 'options'),
Output('sense-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def display_sense_dropdown(word):
if word:
def display_sense_dropdown(word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)

if len(df) >= 1:
Expand Down Expand Up @@ -135,11 +138,12 @@ def display_sense_dropdown(word):
@app.callback(
Output('sense-sample-sentence', 'children'),
Input('sense-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def plot_update_sample_sentence_base_on_sense(sense_value, word):
def plot_update_sample_sentence_base_on_sense(sense_value, word, word_exists):

if word and sense_value:
if word and sense_value and word_exists:
df = get_word_db(API_URL, word) # get_definition_list(word)

if len(df) > 0:
Expand Down Expand Up @@ -181,10 +185,11 @@ def plot_update_sample_sentence_base_on_sense(sense_value, word):
@app.callback(
Output('graph-sense', 'figure'),
Input('sense-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def update_line_chart(sense_value, word):
if word:
def update_line_chart(sense_value, word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)

pos = ''
Expand Down Expand Up @@ -219,10 +224,11 @@ def update_line_chart(sense_value, word):
@app.callback(
Output('source-dropdown', 'options'),
Output('source-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def display_source_dropdown(word):
if word:
def display_source_dropdown(word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)

if len(df) >= 1:
Expand All @@ -244,10 +250,11 @@ def display_source_dropdown(word):
@app.callback(
Output('graph-source', 'figure'),
Input('source-dropdown', 'value'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def update_line_chart(selected_source, word):
if word:
def update_line_chart(selected_source, word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)
pos = ''
if 'pos' in df.columns:
Expand Down
23 changes: 10 additions & 13 deletions src/callbacks/home/sense/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dash import Input, Output, html, MATCH
from dash import Input, Output, State, html, MATCH
from plotly.graph_objs import *
from dash.exceptions import PreventUpdate
from ..api_query import *
Expand All @@ -10,25 +10,22 @@
def init_callback(app, API_URL):
@app.callback(
Output('senses-word', 'children'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def search_word(word):
if word:
df = get_word_db(API_URL, word)

if len(df) >= 1:
return word

return [f'No Word Found: {word}']
def search_word(word, word_exists):
if word and word_exists:
return word

raise PreventUpdate

@app.callback(
Output('senses-container', 'children'),
Input('submitted-word', 'data')
Input('submitted-word', 'data'),
Input('word-exists', 'data')
)
def get_senses(word):
if word:
def get_senses(word, word_exists):
if word and word_exists:
df = get_word_db(API_URL, word)

if len(df) >= 1:
Expand Down
42 changes: 23 additions & 19 deletions src/pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,25 +258,29 @@
dbc.Container([
input_word,

html.Div(
id='home-body-container',
children=[
senses,
html.Br(id='plot-sense-row'),
plot_by_sense,
html.Br(id='plot-source-row'),
html.Br(),
plot_by_source,
html.Br(id='embeddings-row'),
html.Br(),
embeddings,
html.Br(id='network-row'),
network,
html.Br(id='export-row'),
export
],
hidden=True
),
dcc.Loading([
html.Div(id='search-word-error', style={'paddingTop': '10em'}, hidden=True),

html.Div(
id='home-body-container',
children=[
senses,
html.Br(id='plot-sense-row'),
plot_by_sense,
html.Br(id='plot-source-row'),
html.Br(),
plot_by_source,
html.Br(id='embeddings-row'),
html.Br(),
embeddings,
html.Br(id='network-row'),
network,
html.Br(id='export-row'),
export
],
hidden=True
),
])
])
),

Expand Down

0 comments on commit ccf248c

Please sign in to comment.