-
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.
feat(maidr.show): support py-shiny renderer (#67)
- Loading branch information
1 parent
ae50904
commit a944826
Showing
8 changed files
with
146 additions
and
65 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): | ||
@render_maidr | ||
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()) | ||
return s_plot | ||
|
||
|
||
# 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,29 @@ | ||
from __future__ import annotations | ||
|
||
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. | ||
Methods | ||
------- | ||
render() | ||
Asynchronously renders the Maidr object. | ||
""" | ||
|
||
async def render(self) -> Jsonifiable: | ||
"""Return maidr rendered object for a given plot.""" | ||
initial_value = await self.fn() | ||
if initial_value is None: | ||
return None | ||
|
||
maidr_rendered = maidr.render(initial_value) | ||
transformed = await self.transform(maidr_rendered) | ||
return transformed |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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