Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change field type for dates #230

Merged
merged 14 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/datadoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from datadoc.frontend.components.control_bars import build_language_dropdown
from datadoc.frontend.components.control_bars import header
from datadoc.frontend.components.control_bars import progress_bar
from datadoc.frontend.components.dataset_tab import build_dataset_tab
from datadoc.frontend.components.dataset_tab import build_new_dataset_tab
from datadoc.frontend.components.variables_tab import build_variables_tab
from datadoc.logging.logging_config import configure_logging
from datadoc.utils import get_app_version
Expand All @@ -42,8 +42,9 @@
def build_app(app: type[Dash]) -> Dash:
"""Define the layout, register callbacks."""
tabs_children = [
build_dataset_tab(),
# build_dataset_tab(),
build_variables_tab(),
build_new_dataset_tab(),
]

app.layout = dbc.Container(
Expand Down
60 changes: 45 additions & 15 deletions src/datadoc/frontend/callbacks/register_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from dash import Output
from dash import State
from dash import ctx
from dash import no_update

from datadoc import state
from datadoc.enums import SupportedLanguages
Expand All @@ -25,15 +24,19 @@
from datadoc.frontend.callbacks.utils import update_global_language_state
from datadoc.frontend.callbacks.variables import accept_variable_metadata_date_input
from datadoc.frontend.callbacks.variables import accept_variable_metadata_input
from datadoc.frontend.components.builders import build_dataset_edit_section
from datadoc.frontend.components.builders import build_edit_section
from datadoc.frontend.components.builders import build_ssb_accordion
from datadoc.frontend.components.dataset_tab import DATASET_METADATA_INPUT
from datadoc.frontend.components.dataset_tab import build_dataset_metadata_accordion
from datadoc.frontend.components.dataset_tab import SECTION_WRAPPER_ID
from datadoc.frontend.components.variables_tab import ACCORDION_WRAPPER_ID
from datadoc.frontend.components.variables_tab import VARIABLES_INFORMATION_ID
from datadoc.frontend.fields.display_base import VARIABLES_METADATA_DATE_INPUT
from datadoc.frontend.fields.display_base import VARIABLES_METADATA_INPUT
from datadoc.frontend.fields.display_dataset import DISPLAYED_DROPDOWN_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import NON_EDITABLE_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import OBLIGATORY_EDITABLE_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import OPTIONAL_DATASET_METADATA
from datadoc.frontend.fields.display_variables import OBLIGATORY_VARIABLES_METADATA
from datadoc.frontend.fields.display_variables import OPTIONAL_VARIABLES_METADATA
from datadoc.frontend.fields.display_variables import VariableIdentifiers
Expand Down Expand Up @@ -147,20 +150,19 @@ def callback_open_dataset(
"""
return open_dataset_handling(n_clicks, dataset_path)

@app.callback(
Output("dataset-accordion", "children"),
Input("open-button", "n_clicks"),
prevent_initial_call=True,
)
def callback_clear_accordion_values(n_clicks: int) -> list[dbc.AccordionItem]:
"""Recreate accordion items with unique IDs.
# @app.callback(
# Output("dataset-accordion", "children"), # noqa: ERA001
# Input("open-button", "n_clicks"), # noqa: ERA001
# prevent_initial_call=True, # noqa: ERA001)
# def callback_clear_accordion_values(n_clicks: int) -> list[dbc.AccordionItem]:
# """Recreate accordion items with unique IDs.

The purpose is to avoid browser caching and clear the values of all
components inside the dataset accordion when new file is opened
"""
if n_clicks and n_clicks > 0:
return build_dataset_metadata_accordion(n_clicks)
return no_update
# The purpose is to avoid browser caching and clear the values of all
# components inside the dataset accordion when new file is opened
# """
# if n_clicks and n_clicks > 0:
# return build_dataset_metadata_accordion(n_clicks) # noqa: ERA001
# return no_update # noqa: ERA001

@app.callback(
Output(VARIABLES_INFORMATION_ID, "children"),
Expand Down Expand Up @@ -209,6 +211,34 @@ def callback_populate_variables_workspace(
for variable in list(state.metadata.variables)
]

# Work in progress...
@app.callback(
Output(SECTION_WRAPPER_ID, "children"),
Input("language-dropdown", "value"),
prevent_initial_call=True,
)
def callback_populate_dataset_workspace(language: str) -> list:
"""Create dataset workspace with sections."""
update_global_language_state(SupportedLanguages(language))
logger.info("Populating new dataset workspace")
return [
build_dataset_edit_section(
"Obligatorisk",
OBLIGATORY_EDITABLE_DATASET_METADATA,
language,
),
build_dataset_edit_section(
"Anbefalt",
OPTIONAL_DATASET_METADATA,
language,
),
build_dataset_edit_section(
"Maskingenerert",
NON_EDITABLE_DATASET_METADATA,
language,
),
]

@app.callback(
Output(
{
Expand Down
42 changes: 42 additions & 0 deletions src/datadoc/frontend/components/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
from dash import html

from datadoc.frontend.fields.display_base import VARIABLES_METADATA_INPUT
from datadoc.frontend.fields.display_base import DatasetFieldTypes
from datadoc.frontend.fields.display_base import VariablesFieldTypes

if TYPE_CHECKING:
from datadoc_model import model

DATASET_METADATA_INPUT = "dataset-metadata-input"


class AlertTypes(Enum):
"""Types of alerts."""
Expand Down Expand Up @@ -134,6 +137,29 @@ def build_input_field_section(
)


def build_dataset_input_field_section(
metadata_fields: list[DatasetFieldTypes],
language: str,
) -> dbc.Form:
"""Create input fields for dataset."""
return dbc.Form(
[
i.render(
{
"type": "NEW_DATASET",
# "type": DATASET_METADATA_INPUT, # noqa: ERA001
"id": i.identifier,
},
language,
)
for i in metadata_fields
# for i in OBLIGATORY_EDITABLE_DATASET_METADATA
],
id="new-dataset-form",
className="dataset-input-group",
)


def build_edit_section(
metadata_inputs: list,
title: str,
Expand All @@ -151,6 +177,22 @@ def build_edit_section(
)


def build_dataset_edit_section(
title: str,
metadata_inputs: list,
language: str,
) -> html.Section:
"""Create edit section for dataset."""
return html.Section(
id={"type": "dataset-edit-section", "title": title},
children=[
ssb.Title(title, size=3, className="input-section-title"),
build_dataset_input_field_section(metadata_inputs, language),
],
className="dataset-edit-section",
)


def build_ssb_accordion(
header: str,
key: dict,
Expand Down
97 changes: 24 additions & 73 deletions src/datadoc/frontend/components/dataset_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,41 @@

from __future__ import annotations

import dash_bootstrap_components as dbc
from typing import TYPE_CHECKING

import ssb_dash_components as ssb
from dash import html

from datadoc.frontend.components.builders import build_ssb_styled_tab
from datadoc.frontend.fields.display_dataset import NON_EDITABLE_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import OBLIGATORY_EDITABLE_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import OPTIONAL_DATASET_METADATA
from datadoc.frontend.fields.display_dataset import DisplayDatasetMetadata

DATASET_METADATA_INPUT = "dataset-metadata-input"


def build_dataset_metadata_accordion_item(
title: str,
metadata_inputs: list[DisplayDatasetMetadata],
accordion_item_id: str,
) -> dbc.AccordionItem:
"""Build a Dash AccordionItem for the given Metadata inputs with a unique ID.

Typically used to categorize metadata fields.
"""
return dbc.AccordionItem(
title=title,
id=accordion_item_id,
children=[
dbc.Row(
[
dbc.Col(html.Label(i.display_name)),
dbc.Col(
i.component(
placeholder=i.description,
disabled=not i.editable,
id={
"type": DATASET_METADATA_INPUT,
"id": i.identifier,
},
**i.extra_kwargs,
),
width=5,
),
dbc.Col(width=4),
],
)
for i in metadata_inputs
],
)

if TYPE_CHECKING:
import dash_bootstrap_components as dbc

def build_dataset_metadata_accordion(n_clicks: int = 0) -> list[dbc.AccordionItem]:
"""Build the accordion on Dataset metadata tab.

n_clicks parameter is appended to the accordions' items id
to avoid browser caching and refresh the values
"""
obligatory = build_dataset_metadata_accordion_item(
"Obligatorisk",
OBLIGATORY_EDITABLE_DATASET_METADATA,
accordion_item_id=f"obligatory-metadata-accordion-item-{n_clicks}",
)
optional = build_dataset_metadata_accordion_item(
"Anbefalt",
OPTIONAL_DATASET_METADATA,
accordion_item_id=f"optional-metadata-accordion-item-{n_clicks}",
)
non_editable = build_dataset_metadata_accordion_item(
"Maskingenerert",
NON_EDITABLE_DATASET_METADATA,
accordion_item_id=f"non-editable-metadata-accordion-item-{n_clicks}",
)
return [obligatory, optional, non_editable]
DATASET_METADATA_INPUT = "dataset-metadata-input"
SECTION_WRAPPER_ID = "section-wrapper-id"


def build_dataset_tab() -> dbc.Tab:
def build_new_dataset_tab() -> dbc.Tab:
"""Build the Dataset metadata tab."""
return build_ssb_styled_tab(
"Datasett",
dbc.Container(
"Ny Datasett",
html.Article(
[
dbc.Row(html.H2("Datasett detaljer", className="ssb-title")),
dbc.Accordion(
id="dataset-accordion",
always_open=True,
children=build_dataset_metadata_accordion(),
html.Header(
[
ssb.Title(
"Datasett detaljer",
size=2,
className="dataset-title",
),
],
className="dataset-header",
),
html.Article(
id=SECTION_WRAPPER_ID,
className="main-content",
),
],
className="dataset-page-wrapper",
),
)
Loading
Loading