Dive Into PyShiny by Appsilon
Data scientists forget funcitons when writing Shiny UIs
from shiny import App, render, ui
app_ui = ui.page_fluid(
ui.input_slider("n1", "N", 0, 100, 20),
ui.input_slider("n2", "N", 0, 100, 20),
ui.input_slider("n3", "N", 0, 100, 20),
ui.input_slider("n4", "N", 0, 100, 20),
ui.input_slider("n5", "N", 0, 100, 20),
ui.input_slider("n6", "N", 0, 100, 20),
)
app = App(app_ui, None)
from shiny import App, render, ui
def my_slider(id, label):
return ui.input_slider(id, label + "Number", 0, 100, 20)
numbers = ["n1", "n2", "n3", "n4", "n5", "n6"]
labels = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth"]
app_ui = ui.page_fluid(
[my_slider(x, y) for x, y in zip(numbers, labels)]
)
app = App(app_ui, None)
@module.ui
def images_row_ui():
return ui.layout_columns(
ui.column(6, ui.output_image("reference_image")),
ui.column(4, ui.output_image("segmented_image")),
)
@module.server
def images_row_server(
input, output, session, file: SegmentationFile, model: ONNXModel
):
@render.image
def reference_image():
return {"src": SegmentationFile.reference_path}
@render.image
def segmented_image():
prediction = file.get_prediction(model)
img_pred = Image.fromarray(prediction.mask)
path_tmp = NamedTemporaryFile(suffix=".png", delete=False)
img_pred.save(path_tmp.name)
return {"src": path_tmp.name}
@module.ui
def images_row_ui():
return ui.layout_columns(
ui.column(6, ui.output_image("reference_image")),
ui.column(4, ui.output_image("segmented_image")),
)
@module.server
def images_row_server(
input, output, session, file: SegmentationFile, model: ONNXModel
):
@render.image
def reference_image():
return {"src": SegmentationFile.reference_path}
@render.image
def segmented_image():
prediction = file.get_prediction(model)
img_pred = Image.fromarray(prediction.mask)
path_tmp = NamedTemporaryFile(suffix=".png", delete=False)
img_pred.save(path_tmp.name)
return {"src": path_tmp.name}
@module.ui
def images_row_ui():
return ui.layout_columns(
ui.column(6, ui.output_image("reference_image")),
ui.column(4, ui.output_image("segmented_image")),
)
@module.server
def images_row_server(
input, output, session, file: SegmentationFile, model: ONNXModel
):
@render.image
def reference_image():
return {"src": SegmentationFile.reference_path}
@render.image
def segmented_image():
prediction = file.get_prediction(model)
img_pred = Image.fromarray(prediction.mask)
path_tmp = NamedTemporaryFile(suffix=".png", delete=False)
img_pred.save(path_tmp.name)
return {"src": path_tmp.name}
@module.ui
def images_row_ui():
return ui.layout_columns(
ui.column(6, ui.output_image("reference_image")),
ui.column(4, ui.output_image("segmented_image")),
)
@module.server
def images_row_server(
input, output, session, file: SegmentationFile, model: ONNXModel
):
@render.image
def reference_image():
return {"src": SegmentationFile.reference_path}
@render.image
def segmented_image():
prediction = file.get_prediction(model)
img_pred = Image.fromarray(prediction.mask)
path_tmp = NamedTemporaryFile(suffix=".png", delete=False)
img_pred.save(path_tmp.name)
return {"src": path_tmp.name}
@render.ui
def images_rows_ui():
files = req(dataframe_selected_images())
html_tags = []
for i, seg_file in enumerate(files):
# Creating shiny server module works by calling the server function
images_row_server(f"image_row_{i}", seg_file, selected_onnx_model())
html_tags.append(images_row_ui(f"image_row_{i}"))
return ui.TagList(html_tags)