Skip to content

Commit

Permalink
app: add download mapping file support
Browse files Browse the repository at this point in the history
move forms into components
  • Loading branch information
abhidg committed Nov 6, 2024
1 parent a60e819 commit f074a00
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 149 deletions.
177 changes: 28 additions & 149 deletions src/arcmapper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dash_bootstrap_components as dbc


from .components import select
from .components import arc_form, upload_form
from .util import read_upload_data
from .dictionary import read_data_dictionary
from .strategies import map as map_data_dictionary_to_arc
Expand Down Expand Up @@ -43,159 +43,23 @@
dark=True,
)

upload_form = dbc.Container(
output_table = dbc.Container(
html.Div(
dbc.Form(
[
dbc.Row(
[
html.P(
[
html.Strong("SOURCE: "),
"ARCmapper supports data dictionaries in CSV, XLSX, JSON schema, or you can upload sample data.",
]
)
]
),
dbc.Row(
dbc.Col(
dbc.Switch(
id="upload-is-sample-data",
label="Uploaded file is sample data, not a data dictionary. Data will be sent to server, only use on local deployments",
disabled=True,
)
)
),
dbc.Row(
[
dbc.Label("Responses column", width="auto"),
dbc.Col(
dbc.Input(
id="upload-col-responses",
type="text",
value="Choices, Calculations, OR Slider Labels",
),
className="me-3",
),
dbc.Label("Description column", width="auto"),
dbc.Col(
dbc.Input(
id="upload-col-description",
type="text",
placeholder="Defaults to longest column",
value="Field Label",
),
className="me-3",
),
dbc.Col(
dcc.Upload(
id="upload-input-file",
children=html.Div(
"Drag and drop or select file",
style={
"border": "1px dashed silver",
"padding": "0.3em",
},
),
),
className="me-3",
),
dbc.Col(
dbc.Button(
"Upload", id="upload-btn", color="primary", n_clicks=0
)
),
dcc.Store(id="upload-data-dictionary"),
],
className="g-2",
),
dbc.Row(id="upload-status"),
]
),
style={
"border": "1px solid silver",
"border-radius": "0.4em",
"padding": "1em",
},
),
style={"margin-top": "1em"},
dbc.Row(id="output"),
style={"padding": "0.5em", "border": "1px solid silver", "borderRadius": "5px"},
)
)

arc_form = dbc.Container(
html.Div(
dbc.Form(
final_mapping_form = dbc.Container(
dbc.Row(
dbc.Col(
[
dbc.Row(
html.P(
[
html.Strong("TARGET: "),
"Choose the target ARC version and select method and method parameters",
]
)
),
dbc.Row(
[
dbc.Label("Target ARC version", width="auto"),
dbc.Col(
select("arc-version", ["1.0.0", "1.0.1"]),
className="me-3",
),
dbc.Label("Mapping method", width="auto"),
dbc.Col(
dbc.Select(
id="arc-mapping-method",
options=[
{"label": "TF-IDF", "value": "tf-idf"},
{
"label": "Sentence Transformers",
"value": "sbert",
},
],
value="tf-idf",
),
className="me-3",
),
dbc.Label("Number of matches", width="auto"),
dbc.Col(
dbc.Input(
id="arc-num-matches",
type="number",
min=2,
max=10,
step=1,
value=3,
),
),
dbc.Label("Threshold", width="auto"),
dbc.Col(
dbc.Input(
id="arc-threshold",
type="number",
min=0.1,
max=1,
step=0.1,
value=0.3,
),
),
dbc.Col(dbc.Button("Map to ARC", id="map-btn"), width="auto"),
],
className="g-2",
dcc.Download(id="download-mapping"),
dbc.Button(
"Download mapping", id="download-btn", style={"marginTop": "1em"}
),
]
),
style={
"border": "1px solid silver",
"border-radius": "0.4em",
"padding": "1em",
},
),
style={"margin-top": "1em"},
)

output_table = dbc.Container(
html.Div(
dbc.Row(id="output"),
style={"padding": "0.5em", "border": "1px solid silver", "borderRadius": "5px"},
)
)
)

Expand Down Expand Up @@ -308,5 +172,20 @@ def handle_status(data, active_cell):
)


app.layout = html.Div([navbar, upload_form, arc_form, output_table])
@callback(
Output("download-mapping", "data"),
Input("download-btn", "n_clicks"),
State("mapping", "data"),
prevent_initial_call=True,
)
def handle_download(_, data):
if ctx.triggered_id == "download-btn":
df = pd.DataFrame(data)
df = df[df.status == "✅"].drop(columns=["status", "rank"])
return dcc.send_data_frame(df.to_csv, "arcmapper-mapping-file.csv", index=False)
else:
raise dash.exceptions.PreventUpdate


app.layout = html.Div([navbar, upload_form, arc_form, output_table, final_mapping_form])
server = app.server
151 changes: 151 additions & 0 deletions src/arcmapper/components.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dash import html, dcc
import dash_bootstrap_components as dbc


Expand All @@ -7,3 +8,153 @@ def select(id: str, values: list[str], default: str | None = None) -> dbc.Select
value=default if default else values[0],
options=[{"label": v, "value": v} for v in values],
)


upload_form = dbc.Container(
html.Div(
dbc.Form(
[
dbc.Row(
[
html.P(
[
html.Strong("SOURCE: "),
"ARCmapper supports data dictionaries in CSV, XLSX, JSON schema, or you can upload sample data.",
]
)
]
),
dbc.Row(
dbc.Col(
dbc.Switch(
id="upload-is-sample-data",
label="Uploaded file is sample data, not a data dictionary. Data will be sent to server, only use on local deployments",
disabled=True,
)
)
),
dbc.Row(
[
dbc.Label("Responses column", width="auto"),
dbc.Col(
dbc.Input(
id="upload-col-responses",
type="text",
value="Choices, Calculations, OR Slider Labels",
),
className="me-3",
),
dbc.Label("Description column", width="auto"),
dbc.Col(
dbc.Input(
id="upload-col-description",
type="text",
placeholder="Defaults to longest column",
value="Field Label",
),
className="me-3",
),
dbc.Col(
dcc.Upload(
id="upload-input-file",
children=html.Div(
"Drag and drop or select file",
style={
"border": "1px dashed silver",
"padding": "0.3em",
},
),
),
className="me-3",
),
dbc.Col(
dbc.Button(
"Upload", id="upload-btn", color="primary", n_clicks=0
)
),
dcc.Store(id="upload-data-dictionary"),
],
className="g-2",
),
dbc.Row(id="upload-status"),
]
),
style={
"border": "1px solid silver",
"border-radius": "0.4em",
"padding": "1em",
},
),
style={"margin-top": "1em"},
)

arc_form = dbc.Container(
html.Div(
dbc.Form(
[
dbc.Row(
html.P(
[
html.Strong("TARGET: "),
"Choose the target ARC version and select method and method parameters",
]
)
),
dbc.Row(
[
dbc.Label("Target ARC version", width="auto"),
dbc.Col(
select("arc-version", ["1.0.0", "1.0.1"]),
className="me-3",
),
dbc.Label("Mapping method", width="auto"),
dbc.Col(
dbc.Select(
id="arc-mapping-method",
options=[
{"label": "TF-IDF", "value": "tf-idf"},
{
"label": "Sentence Transformers",
"value": "sbert",
},
],
value="tf-idf",
),
className="me-3",
),
dbc.Label("Number of matches", width="auto"),
dbc.Col(
dbc.Input(
id="arc-num-matches",
type="number",
min=2,
max=10,
step=1,
value=3,
),
),
dbc.Label("Threshold", width="auto"),
dbc.Col(
dbc.Input(
id="arc-threshold",
type="number",
min=0.1,
max=1,
step=0.1,
value=0.3,
),
),
dbc.Col(dbc.Button("Map to ARC", id="map-btn"), width="auto"),
],
className="g-2",
),
]
),
style={
"border": "1px solid silver",
"border-radius": "0.4em",
"padding": "1em",
},
),
style={"margin-top": "1em"},
)

0 comments on commit f074a00

Please sign in to comment.