-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: redesigning shiny and reusing _create_html_doc for iframe genera…
…tion
- Loading branch information
1 parent
3f718a7
commit ad37451
Showing
5 changed files
with
141 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import matplotlib.pyplot as plt | ||
import seaborn as sns | ||
from shiny import App, ui | ||
|
||
from maidr.widget.shiny import render_maidr | ||
|
||
# Load the dataset | ||
iris = sns.load_dataset("iris") | ||
|
||
# Define the UI components for the Shiny application | ||
app_ui = ui.page_fluid( | ||
ui.row( | ||
ui.column( | ||
3, | ||
ui.input_select( | ||
"x_var", | ||
"Select X variable:", | ||
choices=iris.select_dtypes(include=["float64"]).columns.tolist(), | ||
selected="sepal_length", | ||
), | ||
ui.input_select( | ||
"y_var", | ||
"Select Y variable:", | ||
choices=iris.select_dtypes(include=["float64"]).columns.tolist(), | ||
selected="sepal_width", | ||
), | ||
), | ||
ui.column(9, ui.output_ui("create_reactivebarplot")), | ||
) | ||
) | ||
|
||
|
||
# Define the server | ||
def server(input, output, session): | ||
def create_reactivebarplot(): | ||
fig, ax = plt.subplots(figsize=(10, 6)) | ||
s_plot = sns.scatterplot( | ||
data=iris, x=input.x_var(), y=input.y_var(), hue="species", ax=ax | ||
) | ||
ax.set_title(f"Iris {input.y_var()} vs {input.x_var()}") | ||
ax.set_xlabel(input.x_var().replace("_", " ").title()) | ||
ax.set_ylabel(input.y_var().replace("_", " ").title()) # Hello | ||
return s_plot | ||
output.reactivebarplot = render_maidr(create_reactivebarplot) | ||
|
||
|
||
# Create the app | ||
app = App(app_ui, server) | ||
|
||
# Run the app | ||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Callable, Coroutine, Optional | ||
|
||
from shiny.render import ui | ||
from shiny.types import Jsonifiable | ||
|
||
import maidr | ||
|
||
|
||
class render_maidr(ui): | ||
""" | ||
A custom UI rendering class for Maidr objects in Shiny applications. | ||
This class extends the Shiny UI rendering functionality to handle Maidr objects. | ||
Attributes | ||
---------- | ||
fn : Callable[[], Coroutine[Any, Any, Optional[Any]]] | ||
An asynchronous function that returns the value to be rendered. | ||
Methods | ||
------- | ||
render() | ||
Asynchronously renders the Maidr object. | ||
""" | ||
|
||
def __init__(self, fn: Callable[[], Coroutine[Any, Any, Optional[Any]]]): | ||
""" | ||
Initialize the render_maidr instance. | ||
Parameters | ||
---------- | ||
fn : Callable[[], Coroutine[Any, Any, Optional[Any]]] | ||
An asynchronous function that returns the value to be rendered. | ||
""" | ||
super().__init__(fn) | ||
|
||
async def render(self) -> Optional[Jsonifiable]: | ||
""" | ||
Asynchronously render the Maidr object. | ||
This method calls the provided function, renders the result using Maidr, | ||
and transforms it into a Jsonifiable format. | ||
Returns | ||
------- | ||
Optional[Jsonifiable] | ||
The rendered and transformed Maidr object, or | ||
None if the initial value is None. | ||
""" | ||
initial_value: Optional[Any] = await self.fn() | ||
if initial_value is None: | ||
return None | ||
|
||
maidr_rendered: Any = maidr.render(initial_value) | ||
transformed: Jsonifiable = await self.transform(maidr_rendered) | ||
return transformed |