Skip to content

Commit

Permalink
split server into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mccalluc committed Sep 26, 2024
1 parent 95c745c commit 1b4f9fc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 74 deletions.
75 changes: 4 additions & 71 deletions dp_creator_ii/app/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
9 changes: 8 additions & 1 deletion dp_creator_ii/app/analysis_panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from shiny import ui
from shiny import ui, reactive


def analysis_ui():
Expand All @@ -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")
26 changes: 25 additions & 1 deletion dp_creator_ii/app/dataset_panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from shiny import ui
from pathlib import Path
import json
from shiny import ui, reactive, render


def dataset_ui():
Expand All @@ -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")
47 changes: 46 additions & 1 deletion dp_creator_ii/app/results_panel.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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

0 comments on commit 1b4f9fc

Please sign in to comment.