Skip to content
Snippets Groups Projects
widgets.py 4.43 KiB
Newer Older
Karine PARRA's avatar
Karine PARRA committed
from ipywidgets import widgets
from datetime import date
from source.simulation_parameters import *
Karine PARRA's avatar
Karine PARRA committed

Karine PARRA's avatar
Karine PARRA committed

params = [f'PRIVATE-TOKEN: {os.environ["NAOS_TOKEN"]}']
studies = get_curl_json('https://forge.naos-cluster.tech/api/v4/groups/594/projects', params)
Karine PARRA's avatar
Karine PARRA committed
style = {'description_width': '200px'}
layout = widgets.Layout(width='auto', height='30px')
layout_dates = widgets.Layout(width='auto', height='30px')
selected_study = 0
loaded_study_datas = None
loaded_study_model_name = ""
loaded_study_model_id = None
loaded_study_sources = None

widget_sources_selected_study = widgets.Text(
Karine PARRA's avatar
Karine PARRA committed
    value='',
    description='Sources : ',
Karine PARRA's avatar
Karine PARRA committed
    layout=layout)

Karine PARRA's avatar
Karine PARRA committed
    value='',
    description='Model : ',
Karine PARRA's avatar
Karine PARRA committed
    layout=layout)

Karine PARRA's avatar
Karine PARRA committed
    value='',
    description='Result : ',
Karine PARRA's avatar
Karine PARRA committed


Karine PARRA's avatar
Karine PARRA committed
    if change['type'] == 'change' and change['name'] == 'value':
        global end_date
        end_date = change['new'].strftime('%Y-%m-%dT%H:%M:%S.%fZ')
Karine PARRA's avatar
Karine PARRA committed


widget_end_date = widgets.DatePicker(
    description='End date :',
    disabled=False,
    value=date.today(),
    style=style,
    layout=layout_dates)

widget_end_date.observe(select_end_date)
end_date = widget_end_date.value.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
Karine PARRA's avatar
Karine PARRA committed


Karine PARRA's avatar
Karine PARRA committed
    if change['type'] == 'change' and change['name'] == 'value':
        global start_date
        start_date = change['new'].strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        widget_end_date.value = change['new'] + timedelta(days=10)
Karine PARRA's avatar
Karine PARRA committed


widget_start_date = widgets.DatePicker(
    description='Study start date :',
    disabled=False,
    value=date.today() - timedelta(days=10),
    style=style,
    layout=layout_dates)
Karine PARRA's avatar
Karine PARRA committed

widget_start_date.observe(select_start_date)
start_date = widget_start_date.value.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
Karine PARRA's avatar
Karine PARRA committed


def select_study(change):
    if change['type'] == 'change' and change['name'] == 'value':
        global selected_study
        global loaded_study_datas
        global loaded_study_model_name
        global loaded_study_model_id
        global loaded_study_sources

        selected_study = change['new']
        loaded_study_datas = get_curl_json(
Karine PARRA's avatar
Karine PARRA committed
            f'https://forge.naos-cluster.tech/api/v4/projects/{selected_study}', params)

        loaded_study_model_name = loaded_study_datas['forked_from_project']['path']
        loaded_study_model_id = loaded_study_datas['forked_from_project']['id']
        loaded_study_sources = ast.literal_eval(get_curl_json(
            f'https://giros-fleuve.jumeaux-numeriques.fr/catalog-api/gitlab/projects/{loaded_study_model_id}'
            f'/repository/files/sources-forcages%2Ejson/raw?ref=main'))
        widget_model_selected_study.value = loaded_study_model_name
        widget_sources_selected_study.value = '["' + '", "'.join(loaded_study_sources) + '"]'
        widget_sources_selected_study.disabled = False
        generate_files_button.disabled = False


widget_studies = widgets.Dropdown(
    options=[(study["name"], study["id"]) for study in studies],
    description='Select a study :',
    value=None,
    style=style,
    layout=layout)
Karine PARRA's avatar
Karine PARRA committed

Karine PARRA's avatar
Karine PARRA committed

generate_files_button = widgets.Button(
    description="Update forcing files",
    disabled=True,
Karine PARRA's avatar
Karine PARRA committed
    layout=layout)


def gererate_files(b):
    widget_result.value = "In progress...."
Karine PARRA's avatar
Karine PARRA committed

    try:
        sources_variables = ast.literal_eval(widget_sources_selected_study.value)

        naos_token = os.environ.get("NAOS_TOKEN")
        write_in_naos = (os.environ.get("WRITE_IN_NAOS") == 'True')

        parameters = SimulationParameters(
            model=loaded_study_model_name,
            project_id=selected_study,
            sources=sources_variables,
            start_date=start_date,
            end_date=end_date,
            naos_token=naos_token,
            write_in_naos=write_in_naos,
        )
        widget_result.value = parameters.create_simulation()["result"]

    except ValueError as err:
        widget_result.value = f"Wrong format for sources. Please use : '[\"source1\", \"source2\"]. Error : {err}"


generate_files_button.on_click(gererate_files)

widgets_list = [widget_studies,
                widget_model_selected_study,
                widget_sources_selected_study,
                widget_start_date,
                widget_end_date,
                generate_files_button,
                widget_result]