From a77a40618dc45117fd2a6ad7c333aa7e70e8c141 Mon Sep 17 00:00:00 2001 From: Ivan Usov Date: Wed, 8 Sep 2021 17:17:17 +0200 Subject: [PATCH] Add Apply button to proposal selection --- pyzebra/__init__.py | 9 +++----- pyzebra/app/app.py | 24 ++++++++++++++++++--- pyzebra/app/panel_ccl_integrate.py | 31 ++++++++++------------------ pyzebra/app/panel_hdf_param_study.py | 27 ++++++++---------------- pyzebra/app/panel_hdf_viewer.py | 27 ++++++++---------------- pyzebra/app/panel_param_study.py | 31 ++++++++++------------------ pyzebra/utils.py | 20 ++++++++++++++++++ 7 files changed, 84 insertions(+), 85 deletions(-) create mode 100644 pyzebra/utils.py diff --git a/pyzebra/__init__.py b/pyzebra/__init__.py index d9e43c6..495ebda 100644 --- a/pyzebra/__init__.py +++ b/pyzebra/__init__.py @@ -1,11 +1,8 @@ from pyzebra.anatric import * from pyzebra.ccl_io import * -from pyzebra.h5 import * -from pyzebra.xtal import * from pyzebra.ccl_process import * - -ZEBRA_PROPOSALS_PATHS = [ - f"/afs/psi.ch/project/sinqdata/{year}/zebra/" for year in (2016, 2017, 2018, 2020, 2021) -] +from pyzebra.h5 import * +from pyzebra.utils import * +from pyzebra.xtal import * __version__ = "0.5.0" diff --git a/pyzebra/app/app.py b/pyzebra/app/app.py index 8a78459..e62a665 100644 --- a/pyzebra/app/app.py +++ b/pyzebra/app/app.py @@ -2,9 +2,10 @@ import logging import sys from io import StringIO +import pyzebra from bokeh.io import curdoc from bokeh.layouts import column, row -from bokeh.models import Panel, Tabs, TextAreaInput, TextInput +from bokeh.models import Button, Panel, Tabs, TextAreaInput, TextInput import panel_ccl_integrate import panel_hdf_anatric @@ -25,15 +26,32 @@ bokeh_logger = logging.getLogger("bokeh") bokeh_logger.addHandler(bokeh_handler) bokeh_log_textareainput = TextAreaInput(title="server output:", height=150) -proposal_textinput = TextInput(title="Proposal number:", width=210) +def proposal_textinput_callback(_attr, _old, _new): + apply_button.disabled = False + +proposal_textinput = TextInput(title="Proposal number:", name="") +proposal_textinput.on_change("value_input", proposal_textinput_callback) doc.proposal_textinput = proposal_textinput +def apply_button_callback(): + try: + proposal_path = pyzebra.find_proposal_path(proposal_textinput.value) + except ValueError as e: + print(e) + return + + proposal_textinput.name = proposal_path + apply_button.disabled = True + +apply_button = Button(label="Apply", button_type="primary") +apply_button.on_click(apply_button_callback) + # Final layout doc.add_root( column( Tabs( tabs=[ - Panel(child=proposal_textinput, title="user config"), + Panel(child=column(proposal_textinput, apply_button), title="user config"), panel_hdf_viewer.create(), panel_hdf_anatric.create(), panel_ccl_integrate.create(), diff --git a/pyzebra/app/panel_ccl_integrate.py b/pyzebra/app/panel_ccl_integrate.py index e92e150..87746b3 100644 --- a/pyzebra/app/panel_ccl_integrate.py +++ b/pyzebra/app/panel_ccl_integrate.py @@ -76,28 +76,19 @@ def create(): js_data = ColumnDataSource(data=dict(content=["", ""], fname=["", ""], ext=["", ""])) def file_select_update_for_proposal(): - proposal = proposal_textinput.value.strip() - if not proposal: + proposal_path = proposal_textinput.name + if proposal_path: + file_list = [] + for file in os.listdir(proposal_path): + if file.endswith((".ccl", ".dat")): + file_list.append((os.path.join(proposal_path, file), file)) + file_select.options = file_list + file_open_button.disabled = False + file_append_button.disabled = False + else: file_select.options = [] file_open_button.disabled = True file_append_button.disabled = True - return - - for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: - proposal_path = os.path.join(zebra_proposals_path, proposal) - if os.path.isdir(proposal_path): - # found it - break - else: - raise ValueError(f"Can not find data for proposal '{proposal}'.") - - file_list = [] - for file in os.listdir(proposal_path): - if file.endswith((".ccl", ".dat")): - file_list.append((os.path.join(proposal_path, file), file)) - file_select.options = file_list - file_open_button.disabled = False - file_append_button.disabled = False doc.add_periodic_callback(file_select_update_for_proposal, 5000) @@ -105,7 +96,7 @@ def create(): file_select_update_for_proposal() proposal_textinput = doc.proposal_textinput - proposal_textinput.on_change("value", proposal_textinput_callback) + proposal_textinput.on_change("name", proposal_textinput_callback) def _init_datatable(): scan_list = [s["idx"] for s in det_data] diff --git a/pyzebra/app/panel_hdf_param_study.py b/pyzebra/app/panel_hdf_param_study.py index e0be84d..a371624 100644 --- a/pyzebra/app/panel_hdf_param_study.py +++ b/pyzebra/app/panel_hdf_param_study.py @@ -57,24 +57,15 @@ def create(): def file_select_update(): if data_source.value == "proposal number": - proposal = proposal_textinput.value.strip() - if not proposal: - file_select.options = [] - return - - for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: - proposal_path = os.path.join(zebra_proposals_path, proposal) - if os.path.isdir(proposal_path): - # found it - break + proposal_path = proposal_textinput.name + if proposal_path: + file_list = [] + for file in os.listdir(proposal_path): + if file.endswith(".hdf"): + file_list.append((os.path.join(proposal_path, file), file)) + file_select.options = file_list else: - raise ValueError(f"Can not find data for proposal '{proposal}'.") - - file_list = [] - for file in os.listdir(proposal_path): - if file.endswith(".hdf"): - file_list.append((os.path.join(proposal_path, file), file)) - file_select.options = file_list + file_select.options = [] else: # "cami file" if not cami_meta: @@ -101,7 +92,7 @@ def create(): file_select_update() proposal_textinput = doc.proposal_textinput - proposal_textinput.on_change("value", proposal_textinput_callback) + proposal_textinput.on_change("name", proposal_textinput_callback) def upload_button_callback(_attr, _old, new): nonlocal cami_meta diff --git a/pyzebra/app/panel_hdf_viewer.py b/pyzebra/app/panel_hdf_viewer.py index a64611b..147c816 100644 --- a/pyzebra/app/panel_hdf_viewer.py +++ b/pyzebra/app/panel_hdf_viewer.py @@ -60,24 +60,15 @@ def create(): def file_select_update(): if data_source.value == "proposal number": - proposal = proposal_textinput.value.strip() - if not proposal: - file_select.options = [] - return - - for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: - proposal_path = os.path.join(zebra_proposals_path, proposal) - if os.path.isdir(proposal_path): - # found it - break + proposal_path = proposal_textinput.name + if proposal_path: + file_list = [] + for file in os.listdir(proposal_path): + if file.endswith(".hdf"): + file_list.append((os.path.join(proposal_path, file), file)) + file_select.options = file_list else: - raise ValueError(f"Can not find data for proposal '{proposal}'.") - - file_list = [] - for file in os.listdir(proposal_path): - if file.endswith(".hdf"): - file_list.append((os.path.join(proposal_path, file), file)) - file_select.options = file_list + file_select.options = [] else: # "cami file" if not cami_meta: @@ -104,7 +95,7 @@ def create(): file_select_update() proposal_textinput = doc.proposal_textinput - proposal_textinput.on_change("value", proposal_textinput_callback) + proposal_textinput.on_change("name", proposal_textinput_callback) def upload_button_callback(_attr, _old, new): nonlocal cami_meta diff --git a/pyzebra/app/panel_param_study.py b/pyzebra/app/panel_param_study.py index d78925b..bccf241 100644 --- a/pyzebra/app/panel_param_study.py +++ b/pyzebra/app/panel_param_study.py @@ -86,28 +86,19 @@ def create(): js_data = ColumnDataSource(data=dict(content=[""], fname=[""], ext=[""])) def file_select_update_for_proposal(): - proposal = proposal_textinput.value.strip() - if not proposal: + proposal_path = proposal_textinput.name + if proposal_path: + file_list = [] + for file in os.listdir(proposal_path): + if file.endswith((".ccl", ".dat")): + file_list.append((os.path.join(proposal_path, file), file)) + file_select.options = file_list + file_open_button.disabled = False + file_append_button.disabled = False + else: file_select.options = [] file_open_button.disabled = True file_append_button.disabled = True - return - - for zebra_proposals_path in pyzebra.ZEBRA_PROPOSALS_PATHS: - proposal_path = os.path.join(zebra_proposals_path, proposal) - if os.path.isdir(proposal_path): - # found it - break - else: - raise ValueError(f"Can not find data for proposal '{proposal}'.") - - file_list = [] - for file in os.listdir(proposal_path): - if file.endswith((".ccl", ".dat")): - file_list.append((os.path.join(proposal_path, file), file)) - file_select.options = file_list - file_open_button.disabled = False - file_append_button.disabled = False doc.add_periodic_callback(file_select_update_for_proposal, 5000) @@ -115,7 +106,7 @@ def create(): file_select_update_for_proposal() proposal_textinput = doc.proposal_textinput - proposal_textinput.on_change("value", proposal_textinput_callback) + proposal_textinput.on_change("name", proposal_textinput_callback) def _init_datatable(): scan_list = [s["idx"] for s in det_data] diff --git a/pyzebra/utils.py b/pyzebra/utils.py new file mode 100644 index 0000000..ba8c0b9 --- /dev/null +++ b/pyzebra/utils.py @@ -0,0 +1,20 @@ +import os + +ZEBRA_PROPOSALS_PATHS = [ + f"/afs/psi.ch/project/sinqdata/{year}/zebra/" for year in (2016, 2017, 2018, 2020, 2021) +] + +def find_proposal_path(proposal): + proposal = proposal.strip() + if proposal: + for zebra_proposals_path in ZEBRA_PROPOSALS_PATHS: + proposal_path = os.path.join(zebra_proposals_path, proposal) + if os.path.isdir(proposal_path): + # found it + break + else: + raise ValueError(f"Can not find data for proposal '{proposal}'.") + else: + proposal_path = "" + + return proposal_path