From 1b4f9fcef3bf6d3552157675b890d1ca556d6607 Mon Sep 17 00:00:00 2001 From: Chuck McCallum Date: Thu, 26 Sep 2024 15:44:33 -0400 Subject: [PATCH] split server into functions --- dp_creator_ii/app/__init__.py | 75 ++--------------------------- dp_creator_ii/app/analysis_panel.py | 9 +++- dp_creator_ii/app/dataset_panel.py | 26 +++++++++- dp_creator_ii/app/results_panel.py | 47 +++++++++++++++++- 4 files changed, 83 insertions(+), 74 deletions(-) diff --git a/dp_creator_ii/app/__init__.py b/dp_creator_ii/app/__init__.py index ece1e8c..f636d97 100644 --- a/dp_creator_ii/app/__init__.py +++ b/dp_creator_ii/app/__init__.py @@ -1,10 +1,4 @@ -import json -from pathlib import Path - -from shiny import App, ui, reactive, render - -from dp_creator_ii.template import make_notebook_py, make_script_py -from dp_creator_ii.converters import convert_py_to_nb +from shiny import App, ui from dp_creator_ii.app import analysis_panel, dataset_panel, results_panel @@ -21,70 +15,9 @@ def server(input, output, session): - config_path = Path(__file__).parent / "config.json" - config = json.loads(config_path.read_text()) - config_path.unlink() - - csv_path = reactive.value(config["csv_path"]) - unit_of_privacy = reactive.value(config["unit_of_privacy"]) - - @render.text - def csv_path_text(): - return str(csv_path.get()) - - @render.text - def unit_of_privacy_text(): - return str(unit_of_privacy.get()) - - @reactive.effect - @reactive.event(input.go_to_analysis) - def go_to_analysis(): - ui.update_navs("top_level_nav", selected="analysis_panel") - - @reactive.effect - @reactive.event(input.go_to_results) - def go_to_results(): - ui.update_navs("top_level_nav", selected="results_panel") - - @render.download( - filename="dp-creator-script.py", - media_type="text/x-python", - ) - async def download_script(): - script_py = make_script_py( - unit=1, - loss=1, - weights=[1], - ) - yield script_py - - @render.download( - filename="dp-creator-notebook.ipynb", - media_type="application/x-ipynb+json", - ) - async def download_notebook_unexecuted(): - notebook_py = make_notebook_py( - csv_path="todo.csv", - unit=1, - loss=1, - weights=[1], - ) - notebook_nb = convert_py_to_nb(notebook_py) - yield notebook_nb - - @render.download( - filename="dp-creator-notebook-executed.ipynb", - media_type="application/x-ipynb+json", - ) - async def download_notebook_executed(): - notebook_py = make_notebook_py( - csv_path="todo.csv", - unit=1, - loss=1, - weights=[1], - ) - notebook_nb = convert_py_to_nb(notebook_py, execute=True) - yield notebook_nb + dataset_panel.dataset_server(input, output, session) + analysis_panel.analysis_server(input, output, session) + results_panel.results_server(input, output, session) app = App(app_ui, server) diff --git a/dp_creator_ii/app/analysis_panel.py b/dp_creator_ii/app/analysis_panel.py index 08f278f..ff89a81 100644 --- a/dp_creator_ii/app/analysis_panel.py +++ b/dp_creator_ii/app/analysis_panel.py @@ -1,4 +1,4 @@ -from shiny import ui +from shiny import ui, reactive def analysis_ui(): @@ -8,3 +8,10 @@ def analysis_ui(): ui.input_action_button("go_to_results", "Download results"), value="analysis_panel", ) + + +def analysis_server(input, output, session): + @reactive.effect + @reactive.event(input.go_to_results) + def go_to_results(): + ui.update_navs("top_level_nav", selected="results_panel") diff --git a/dp_creator_ii/app/dataset_panel.py b/dp_creator_ii/app/dataset_panel.py index e53ed2b..8e83995 100644 --- a/dp_creator_ii/app/dataset_panel.py +++ b/dp_creator_ii/app/dataset_panel.py @@ -1,4 +1,6 @@ -from shiny import ui +from pathlib import Path +import json +from shiny import ui, reactive, render def dataset_ui(): @@ -10,3 +12,25 @@ def dataset_ui(): ui.input_action_button("go_to_analysis", "Perform analysis"), value="dataset_panel", ) + + +def dataset_server(input, output, session): + config_path = Path(__file__).parent / "config.json" + config = json.loads(config_path.read_text()) + config_path.unlink() + + csv_path = reactive.value(config["csv_path"]) + unit_of_privacy = reactive.value(config["unit_of_privacy"]) + + @render.text + def csv_path_text(): + return str(csv_path.get()) + + @render.text + def unit_of_privacy_text(): + return str(unit_of_privacy.get()) + + @reactive.effect + @reactive.event(input.go_to_analysis) + def go_to_analysis(): + ui.update_navs("top_level_nav", selected="analysis_panel") diff --git a/dp_creator_ii/app/results_panel.py b/dp_creator_ii/app/results_panel.py index c2eb1a2..aa72abf 100644 --- a/dp_creator_ii/app/results_panel.py +++ b/dp_creator_ii/app/results_panel.py @@ -1,4 +1,7 @@ -from shiny import ui +from shiny import ui, render + +from dp_creator_ii.template import make_notebook_py, make_script_py +from dp_creator_ii.converters import convert_py_to_nb def results_ui(): @@ -15,3 +18,45 @@ def results_ui(): # ) value="results_panel", ) + + +def results_server(input, output, session): + @render.download( + filename="dp-creator-script.py", + media_type="text/x-python", + ) + async def download_script(): + script_py = make_script_py( + unit=1, + loss=1, + weights=[1], + ) + yield script_py + + @render.download( + filename="dp-creator-notebook.ipynb", + media_type="application/x-ipynb+json", + ) + async def download_notebook_unexecuted(): + notebook_py = make_notebook_py( + csv_path="todo.csv", + unit=1, + loss=1, + weights=[1], + ) + notebook_nb = convert_py_to_nb(notebook_py) + yield notebook_nb + + @render.download( + filename="dp-creator-notebook-executed.ipynb", + media_type="application/x-ipynb+json", + ) + async def download_notebook_executed(): + notebook_py = make_notebook_py( + csv_path="todo.csv", + unit=1, + loss=1, + weights=[1], + ) + notebook_nb = convert_py_to_nb(notebook_py, execute=True) + yield notebook_nb