Skip to content

Commit

Permalink
Merge pull request #1 from posit-dev/TPTM-bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes for Shiny for Python Exercises
  • Loading branch information
mikeckennedy authored Jul 10, 2024
2 parents ca28664 + d253d4c commit e6fa6bd
Show file tree
Hide file tree
Showing 18 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
account_subset = df[df["account"] == input.account()]
if input.metric() == "ROC Curve":
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
account_subset = df[df["account"] == input.account()]
if input.metric() == "ROC Curve":
return plot_auc_curve(
Expand Down
2 changes: 1 addition & 1 deletion apps/problem-sets/2-basic-ui/2.4-layout-columns/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
account_subset = df[df["account"] == input.account()]
if input.metric() == "ROC Curve":
return plot_auc_curve(account_subset, "is_electronics", "training_score")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def account_data():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
# `account_data` is called similar to an input.
if input.metric() == "ROC Curve":
return plot_auc_curve(
Expand Down
2 changes: 1 addition & 1 deletion apps/problem-sets/3-reactivity/3.1-reactive-calc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
account_subset = df[df["account"] == input.account()]
if input.metric() == "ROC Curve":
return plot_auc_curve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ We have a second sidebar input which allows the user to filter the dataset by th
Add a second reactive calculation to the app which filters the `account_data()` reactive.

For reference `input.chars()` returns a tuple with the lower and upper range of a value, and you can filter the data frame with:
`df[df["text"].str.len().between(*input.chars()]`.
`df[df["text"].str.len().between(*input.chars())]`.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def character_filter():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def account_data():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(account_data, "is_electronics", "training_score")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def sub_selector():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
2 changes: 1 addition & 1 deletion apps/problem-sets/4-dynamic-ui/4.1-render-express/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def character_filter():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add a `ui.input_action_button` and a `@reactive.event` decorator to make app only update when the button is clicked.
Add a `ui.input_action_button` and a `@reactive.event` decorator to make the app only update when the button is clicked.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def sub_selector():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def sub_selector():
with ui.card():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Use `render.express` to create a second dropdown which only contains the unique `sub_account` values of the filtered data.
Add a `ui.input_action_button` and a `@reactive.event` decorator to make a button that, when clicked, resets the slider to its initial values.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def sub_selector():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def sub_selector():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
2 changes: 1 addition & 1 deletion apps/target-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def sub_selector():
ui.card_header("Model Metrics")

@render_plotly
def metric():
def metric_plot():
if input.metric() == "ROC Curve":
return plot_auc_curve(
character_filter(), "is_electronics", "training_score"
Expand Down
17 changes: 8 additions & 9 deletions index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,30 @@ This site is a companion to the [Talk Python video course](https://training.talk
This course assumes that you have a basic understanding of Python, but does not require any knowledge of web applications, JavaScript, or HTML.
If you can read a CSV, manipulate data, and draw a plot in Python, then you have everything you need to start building a Shiny application.


# Setup

There are two ways to work through the examples and exercises in this workshop.

## 1) In your web browser with Shinylive

Shinylive allows you to run full-featured Shiny apps in your browser, and includes a basic editor that is good enough to run examples.
As a result, you should be able to work through all of the examples in the workshop using just the browser without installing anything locally.
Shinylive allows you to run full-featured shiny apps in your browser, and includes a basic editor which is good enough to run examples.
As a result you should be able to work through all of the examples in the workshop using just the browser without installing anything locally.

## 2) Locally with VS Code

While Shinylive is great, it likely isn't the environment you'll use to develop Shiny apps, and so it makes sense to set up VS Code and run the examples locally. To do this, follow these steps before the workshop:

1) Install [VS Code](https://code.visualstudio.com/)
2) Install the Shiny for Python [VS Code extension](https://marketplace.visualstudio.com/items?itemName=posit.shiny-python)
3) Clone the repository with `git clone https://github.com/talkpython/reactive-web-dashboards-with-shiny-course.git`, or alternately download the repository as a zip file: \
2) Install the [Shiny extension for VS Code](https://marketplace.visualstudio.com/items?itemName=posit.shiny)
3) Clone the repository with `git clone https://github.com/rstudio/shiny-python-workshop-2023.git`, or alternately download the repository as a zip file: \
![](images/git-download-button.png){width="287"}
4) Navigate to the project directory and create a new virtual environment with `python3 -m venv .venv`
5) Set your Python interpreter to the virtual environment with `CMD + SHIFT + P` > `Select Interpreter`
6) Open a new terminal prompt, which should switch to `(.venv)`
7) Install the relevant packages with `pip install -r requirements.txt`

All of the example apps are stored in the `/apps` directory.
The examples are in `apps/examples` and the problem sets are in `apps/problem-sets`.
If you've installed the Shiny for Python VS Code extension, you can run any of the apps by opening the `app.py` file and clicking the play button in the top right. ([See screenshot](https://camo.githubusercontent.com/5d947e6dff7d74fd1cf221e79583105c42e4986ae673ce79733ce5edbfdcdda5/68747470733a2f2f7368696e792e7273747564696f2e636f6d2f70792f646f63732f6173736574732f7673636f64652e706e67))
All of the example apps are stored in the `/apps` directory.
The examples are in `apps/examples` and the problem sets are in `apps/problem-sets`.
If you've installed the Shiny extension for VS Code, you can run any of the apps by opening the `app.py` file and clicking the play button in the top right. ([See screenshot](https://camo.githubusercontent.com/5d947e6dff7d74fd1cf221e79583105c42e4986ae673ce79733ce5edbfdcdda5/68747470733a2f2f7368696e792e7273747564696f2e636f6d2f70792f646f63732f6173736574732f7673636f64652e706e67))

Alternatively, run them from the command line with `shiny run <path-to-app.py> --reload`.
Alternatively, run them from the command line with `shiny run <path-to-app.py> --reload`.

0 comments on commit e6fa6bd

Please sign in to comment.