Skip to content

Commit

Permalink
config storage
Browse files Browse the repository at this point in the history
  • Loading branch information
interrogator committed Aug 21, 2019
1 parent c3c7f29 commit 7ca453f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 10 additions & 9 deletions buzzword/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

from .parts import start, explore # noqa: F401
Expand All @@ -35,8 +35,9 @@ def _get_layout():
tables_store = dcc.Store(id="session-tables", data=dict())
click_clear = dcc.Store(id="session-clicks-clear", data=-1)
click_table = dcc.Store(id="session-clicks-table", data=-1)
configs = dcc.Store(id="uploaded-configs", data=dict())
content = html.Div(id="page-content")
stores = [search_store, tables_store, click_clear, click_table]
stores = [search_store, tables_store, click_clear, click_table, configs]
return html.Div([loc] + stores + [content])


Expand Down Expand Up @@ -69,15 +70,15 @@ def _populate_explore_layouts():
LAYOUTS[slug] = _make_explore_layout(slug, meta)


def _get_explore_layout(slug):
def _get_explore_layout(slug, uploaded_configs):
"""
Get (and maybe generate) the explore layout for this slug
"""
from buzzword.parts.start import CORPORA_CONFIGS
if slug not in CORPORA_CONFIGS:
upped = uploaded_configs.get(slug)
conf = CORPORA_CONFIGS.get(slug, upped)
if not conf:
return
conf = CORPORA_CONFIGS[slug]
name = conf["corpus_name"]
# store the default explore for each corpus in a dict for speed
if slug in LAYOUTS:
return LAYOUTS[slug]
Expand All @@ -86,8 +87,8 @@ def _get_explore_layout(slug):
return layout


@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def _choose_correct_page(pathname):
@app.callback(Output("page-content", "children"), [Input("url", "pathname")], [State("uploaded-configs", "data")])
def _choose_correct_page(pathname, configs):
"""
When the URL changes, get correct page and populate page-content with it
"""
Expand All @@ -99,7 +100,7 @@ def _choose_correct_page(pathname):
# if corpus not found, redirect
if slug not in CORPORA:
pathname = ""
layout = _get_explore_layout(slug)
layout = _get_explore_layout(slug, configs)
if layout:
return layout
print("LAYOUT ERROR: ", slug, list(CORPORA.keys()))
Expand Down
14 changes: 9 additions & 5 deletions buzzword/parts/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def _validate_input(contents, names, corpus_name, slug):
Output("dialog-upload", "displayed"),
Output("dialog-upload", "message"),
Output("corpus-table", "children"),
Output("uploaded-configs", "data"),
],
[Input("upload-parse-button", "n_clicks")],
[
Expand All @@ -201,9 +202,10 @@ def _validate_input(contents, names, corpus_name, slug):
State("corpus-language", "value"),
State("upload-corpus-name", "value"),
State("corpus-table", "children"),
State("uploaded-configs", "data"),
],
)
def _upload_files(n_clicks, contents, names, corpus_lang, corpus_name, table_rows):
def _upload_files(n_clicks, contents, names, corpus_lang, corpus_name, table_rows, uploaded_configs):
"""
Callback when the user clicks 'upload and parse'
"""
Expand All @@ -216,7 +218,7 @@ def _upload_files(n_clicks, contents, names, corpus_lang, corpus_name, table_row
msg = _validate_input(contents, names, corpus_name, slug)

if msg:
return bool(msg), msg, table_rows
return bool(msg), msg, table_rows, uploaded_configs

path, is_parsed, size = _store_corpus(contents, names, slug)
corpus = Corpus(path)
Expand All @@ -226,7 +228,7 @@ def _upload_files(n_clicks, contents, names, corpus_lang, corpus_name, table_row
except Exception as error:
msg = f"Problem when parsing the corpus: {str(error)}"
traceback.print_exc()
return bool(msg), msg, table_rows
return bool(msg), msg, table_rows, uploaded_configs

corpus = corpus.load()

Expand Down Expand Up @@ -261,7 +263,8 @@ def _upload_files(n_clicks, contents, names, corpus_lang, corpus_name, table_row
# put it at start of table :)
row = _make_row(OrderedDict(tups), 0, upload=True)
table_rows = [table_rows[0], row] + table_rows[1:]
return bool(msg), msg, table_rows
uploaded_configs[slug] = conf
return bool(msg), msg, table_rows, uploaded_configs


@app.callback(
Expand All @@ -281,7 +284,6 @@ def show_uploaded(contents, filenames):
markdown += f"\n* and {rest} more ..."
return dcc.Markdown(markdown)


header = html.H2("buzzword: a tool for analysing annotated linguistic data")

# if no corpora available, do not show this table
Expand Down Expand Up @@ -310,6 +312,8 @@ def show_uploaded(contents, filenames):

upload = _make_upload_parse_space()

hide = {"display": "none"}

content = [header, intro, uphead, upload_text, upload]
if not is_empty:
content.append(demos)
Expand Down

0 comments on commit 7ca453f

Please sign in to comment.