Skip to content

Commit

Permalink
Port exercises 305 and 306 to shiny express
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Aug 1, 2024
1 parent 7bb4266 commit ab2474f
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 134 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"title":"503 Extract function","markdown":{"yaml":{"title":"503 Extract function"},"headingText":"| echo: false","containsRefs":false,"markdown":"\n\n```{python}\n# | output: asis\nimport sys\nhelpers_path = \"../\"\nif helpers_path not in sys.path:\n sys.path.append(helpers_path)\nfrom helpers import problem_tabs_express, getcwd\n\nproblem_tabs_express(getcwd())\n```\n","srcMarkdownNoYaml":"\n\n```{python}\n# | echo: false\n# | output: asis\nimport sys\nhelpers_path = \"../\"\nif helpers_path not in sys.path:\n sys.path.append(helpers_path)\nfrom helpers import problem_tabs_express, getcwd\n\nproblem_tabs_express(getcwd())\n```\n"},"formats":{"html":{"identifier":{"display-name":"HTML","target-format":"html","base-format":"html"},"execute":{"fig-width":7,"fig-height":5,"fig-format":"retina","fig-dpi":96,"df-print":"default","error":false,"eval":true,"cache":null,"freeze":"auto","echo":true,"output":true,"warning":true,"include":true,"keep-md":false,"keep-ipynb":false,"ipynb":null,"enabled":null,"daemon":null,"daemon-restart":false,"debug":false,"ipynb-filters":[],"ipynb-shell-interactivity":null,"plotly-connected":true,"engine":"jupyter"},"render":{"keep-tex":false,"keep-typ":false,"keep-source":false,"keep-hidden":false,"prefer-html":false,"output-divs":true,"output-ext":"html","fig-align":"default","fig-pos":null,"fig-env":null,"code-fold":"none","code-overflow":"scroll","code-link":false,"code-line-numbers":false,"code-tools":false,"tbl-colwidths":"auto","merge-includes":true,"inline-includes":false,"preserve-yaml":false,"latex-auto-mk":true,"latex-auto-install":true,"latex-clean":true,"latex-min-runs":1,"latex-max-runs":10,"latex-makeindex":"makeindex","latex-makeindex-opts":[],"latex-tlmgr-opts":[],"latex-input-paths":[],"latex-output-dir":null,"link-external-icon":false,"link-external-newwindow":false,"self-contained-math":false,"format-resources":[],"notebook-links":true},"pandoc":{"standalone":true,"wrap":"none","default-image-extension":"png","to":"html","filters":["shinylive"],"css":["../../../styles.css"],"toc":true,"output-file":"index.html"},"language":{"toc-title-document":"Table of contents","toc-title-website":"On this page","related-formats-title":"Other Formats","related-notebooks-title":"Notebooks","source-notebooks-prefix":"Source","other-links-title":"Other Links","code-links-title":"Code Links","launch-dev-container-title":"Launch Dev Container","launch-binder-title":"Launch Binder","article-notebook-label":"Article Notebook","notebook-preview-download":"Download Notebook","notebook-preview-download-src":"Download Source","notebook-preview-back":"Back to Article","manuscript-meca-bundle":"MECA Bundle","section-title-abstract":"Abstract","section-title-appendices":"Appendices","section-title-footnotes":"Footnotes","section-title-references":"References","section-title-reuse":"Reuse","section-title-copyright":"Copyright","section-title-citation":"Citation","appendix-attribution-cite-as":"For attribution, please cite this work as:","appendix-attribution-bibtex":"BibTeX citation:","appendix-view-license":"View License","title-block-author-single":"Author","title-block-author-plural":"Authors","title-block-affiliation-single":"Affiliation","title-block-affiliation-plural":"Affiliations","title-block-published":"Published","title-block-modified":"Modified","title-block-keywords":"Keywords","callout-tip-title":"Tip","callout-note-title":"Note","callout-warning-title":"Warning","callout-important-title":"Important","callout-caution-title":"Caution","code-summary":"Code","code-tools-menu-caption":"Code","code-tools-show-all-code":"Show All Code","code-tools-hide-all-code":"Hide All Code","code-tools-view-source":"View Source","code-tools-source-code":"Source Code","tools-share":"Share","tools-download":"Download","code-line":"Line","code-lines":"Lines","copy-button-tooltip":"Copy to Clipboard","copy-button-tooltip-success":"Copied!","repo-action-links-edit":"Edit this page","repo-action-links-source":"View source","repo-action-links-issue":"Report an issue","back-to-top":"Back to top","search-no-results-text":"No results","search-matching-documents-text":"matching documents","search-copy-link-title":"Copy link to search","search-hide-matches-text":"Hide additional matches","search-more-match-text":"more match in this document","search-more-matches-text":"more matches in this document","search-clear-button-title":"Clear","search-text-placeholder":"","search-detached-cancel-button-title":"Cancel","search-submit-button-title":"Submit","search-label":"Search","toggle-section":"Toggle section","toggle-sidebar":"Toggle sidebar navigation","toggle-dark-mode":"Toggle dark mode","toggle-reader-mode":"Toggle reader mode","toggle-navigation":"Toggle navigation","crossref-fig-title":"Figure","crossref-tbl-title":"Table","crossref-lst-title":"Listing","crossref-thm-title":"Theorem","crossref-lem-title":"Lemma","crossref-cor-title":"Corollary","crossref-prp-title":"Proposition","crossref-cnj-title":"Conjecture","crossref-def-title":"Definition","crossref-exm-title":"Example","crossref-exr-title":"Exercise","crossref-ch-prefix":"Chapter","crossref-apx-prefix":"Appendix","crossref-sec-prefix":"Section","crossref-eq-prefix":"Equation","crossref-lof-title":"List of Figures","crossref-lot-title":"List of Tables","crossref-lol-title":"List of Listings","environment-proof-title":"Proof","environment-remark-title":"Remark","environment-solution-title":"Solution","listing-page-order-by":"Order By","listing-page-order-by-default":"Default","listing-page-order-by-date-asc":"Oldest","listing-page-order-by-date-desc":"Newest","listing-page-order-by-number-desc":"High to Low","listing-page-order-by-number-asc":"Low to High","listing-page-field-date":"Date","listing-page-field-title":"Title","listing-page-field-description":"Description","listing-page-field-author":"Author","listing-page-field-filename":"File Name","listing-page-field-filemodified":"Modified","listing-page-field-subtitle":"Subtitle","listing-page-field-readingtime":"Reading Time","listing-page-field-wordcount":"Word Count","listing-page-field-categories":"Categories","listing-page-minutes-compact":"{0} min","listing-page-category-all":"All","listing-page-no-matches":"No matching items","listing-page-words":"{0} words","listing-page-filter":"Filter","draft":"Draft"},"metadata":{"lang":"en","fig-responsive":true,"quarto-version":"1.5.55","theme":"materia","title":"503 Extract function"},"extensions":{"book":{"multiFile":true}}}},"projectFormats":["html"]}
{"title":"503 Extract function","markdown":{"yaml":{"title":"306 Extract function"},"headingText":"| echo: false","containsRefs":false,"markdown":"\n\n```{python}\n# | output: asis\nimport sys\nhelpers_path = \"../\"\nif helpers_path not in sys.path:\n sys.path.append(helpers_path)\nfrom helpers import problem_tabs_express, getcwd\n\nproblem_tabs_express(getcwd())\n```\n","srcMarkdownNoYaml":"\n\n```{python}\n# | echo: false\n# | output: asis\nimport sys\nhelpers_path = \"../\"\nif helpers_path not in sys.path:\n sys.path.append(helpers_path)\nfrom helpers import problem_tabs_express, getcwd\n\nproblem_tabs_express(getcwd())\n```\n"},"formats":{"html":{"identifier":{"display-name":"HTML","target-format":"html","base-format":"html"},"execute":{"fig-width":7,"fig-height":5,"fig-format":"retina","fig-dpi":96,"df-print":"default","error":false,"eval":true,"cache":null,"freeze":"auto","echo":true,"output":true,"warning":true,"include":true,"keep-md":false,"keep-ipynb":false,"ipynb":null,"enabled":null,"daemon":null,"daemon-restart":false,"debug":false,"ipynb-filters":[],"ipynb-shell-interactivity":null,"plotly-connected":true,"engine":"jupyter"},"render":{"keep-tex":false,"keep-typ":false,"keep-source":false,"keep-hidden":false,"prefer-html":false,"output-divs":true,"output-ext":"html","fig-align":"default","fig-pos":null,"fig-env":null,"code-fold":"none","code-overflow":"scroll","code-link":false,"code-line-numbers":false,"code-tools":false,"tbl-colwidths":"auto","merge-includes":true,"inline-includes":false,"preserve-yaml":false,"latex-auto-mk":true,"latex-auto-install":true,"latex-clean":true,"latex-min-runs":1,"latex-max-runs":10,"latex-makeindex":"makeindex","latex-makeindex-opts":[],"latex-tlmgr-opts":[],"latex-input-paths":[],"latex-output-dir":null,"link-external-icon":false,"link-external-newwindow":false,"self-contained-math":false,"format-resources":[],"notebook-links":true},"pandoc":{"standalone":true,"wrap":"none","default-image-extension":"png","to":"html","filters":["shinylive"],"css":["../../../styles.css"],"toc":true,"output-file":"index.html"},"language":{"toc-title-document":"Table of contents","toc-title-website":"On this page","related-formats-title":"Other Formats","related-notebooks-title":"Notebooks","source-notebooks-prefix":"Source","other-links-title":"Other Links","code-links-title":"Code Links","launch-dev-container-title":"Launch Dev Container","launch-binder-title":"Launch Binder","article-notebook-label":"Article Notebook","notebook-preview-download":"Download Notebook","notebook-preview-download-src":"Download Source","notebook-preview-back":"Back to Article","manuscript-meca-bundle":"MECA Bundle","section-title-abstract":"Abstract","section-title-appendices":"Appendices","section-title-footnotes":"Footnotes","section-title-references":"References","section-title-reuse":"Reuse","section-title-copyright":"Copyright","section-title-citation":"Citation","appendix-attribution-cite-as":"For attribution, please cite this work as:","appendix-attribution-bibtex":"BibTeX citation:","appendix-view-license":"View License","title-block-author-single":"Author","title-block-author-plural":"Authors","title-block-affiliation-single":"Affiliation","title-block-affiliation-plural":"Affiliations","title-block-published":"Published","title-block-modified":"Modified","title-block-keywords":"Keywords","callout-tip-title":"Tip","callout-note-title":"Note","callout-warning-title":"Warning","callout-important-title":"Important","callout-caution-title":"Caution","code-summary":"Code","code-tools-menu-caption":"Code","code-tools-show-all-code":"Show All Code","code-tools-hide-all-code":"Hide All Code","code-tools-view-source":"View Source","code-tools-source-code":"Source Code","tools-share":"Share","tools-download":"Download","code-line":"Line","code-lines":"Lines","copy-button-tooltip":"Copy to Clipboard","copy-button-tooltip-success":"Copied!","repo-action-links-edit":"Edit this page","repo-action-links-source":"View source","repo-action-links-issue":"Report an issue","back-to-top":"Back to top","search-no-results-text":"No results","search-matching-documents-text":"matching documents","search-copy-link-title":"Copy link to search","search-hide-matches-text":"Hide additional matches","search-more-match-text":"more match in this document","search-more-matches-text":"more matches in this document","search-clear-button-title":"Clear","search-text-placeholder":"","search-detached-cancel-button-title":"Cancel","search-submit-button-title":"Submit","search-label":"Search","toggle-section":"Toggle section","toggle-sidebar":"Toggle sidebar navigation","toggle-dark-mode":"Toggle dark mode","toggle-reader-mode":"Toggle reader mode","toggle-navigation":"Toggle navigation","crossref-fig-title":"Figure","crossref-tbl-title":"Table","crossref-lst-title":"Listing","crossref-thm-title":"Theorem","crossref-lem-title":"Lemma","crossref-cor-title":"Corollary","crossref-prp-title":"Proposition","crossref-cnj-title":"Conjecture","crossref-def-title":"Definition","crossref-exm-title":"Example","crossref-exr-title":"Exercise","crossref-ch-prefix":"Chapter","crossref-apx-prefix":"Appendix","crossref-sec-prefix":"Section","crossref-eq-prefix":"Equation","crossref-lof-title":"List of Figures","crossref-lot-title":"List of Tables","crossref-lol-title":"List of Listings","environment-proof-title":"Proof","environment-remark-title":"Remark","environment-solution-title":"Solution","listing-page-order-by":"Order By","listing-page-order-by-default":"Default","listing-page-order-by-date-asc":"Oldest","listing-page-order-by-date-desc":"Newest","listing-page-order-by-number-desc":"High to Low","listing-page-order-by-number-asc":"Low to High","listing-page-field-date":"Date","listing-page-field-title":"Title","listing-page-field-description":"Description","listing-page-field-author":"Author","listing-page-field-filename":"File Name","listing-page-field-filemodified":"Modified","listing-page-field-subtitle":"Subtitle","listing-page-field-readingtime":"Reading Time","listing-page-field-wordcount":"Word Count","listing-page-field-categories":"Categories","listing-page-minutes-compact":"{0} min","listing-page-category-all":"All","listing-page-no-matches":"No matching items","listing-page-words":"{0} words","listing-page-filter":"Filter","draft":"Draft"},"metadata":{"lang":"en","fig-responsive":true,"quarto-version":"1.5.55","theme":"materia","title":"503 Extract function"},"extensions":{"book":{"multiFile":true}}}},"projectFormats":["html"]}
2 changes: 1 addition & 1 deletion .quarto/xref/17a0de79
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/1d2541b4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/38800f1f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/4e54b890
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/6c1c68e5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/6d6497ae
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/8a83b101
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/9c26a204
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/9d9c4186
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":["background","prior-to-traveling","on-the-day","setup","in-your-web-browser-with-shinylive","optional-on-your-laptop-with-vs-code-or-positron"]}
{"headings":["background","prior-to-traveling","on-the-day","setup","in-your-web-browser-with-shinylive","optional-on-your-laptop-with-vs-code-or-positron"],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/b8d84f05
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/cebd9737
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/da19815f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":["overview"],"entries":[]}
{"entries":[],"headings":["overview"]}
2 changes: 1 addition & 1 deletion .quarto/xref/e7f2a448
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"entries":[],"headings":[]}
{"headings":[],"entries":[]}
2 changes: 1 addition & 1 deletion .quarto/xref/f643bb46
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"headings":[],"entries":[]}
{"entries":[],"headings":[]}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def account_data():
(df["account"] == input.account()) & (df["sub_account"] == input.sub_account())
]


@reactive.calc()
def character_filter():
return account_data()[(account_data()["text"].str.len().between(*input.chars()))]
Expand Down
2 changes: 1 addition & 1 deletion docs/exercises/express-306-navset/index.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "503 Extract function"
title: "306 Extract function"
---

```{python}
Expand Down
2 changes: 1 addition & 1 deletion docs/exercises/express-306-navset/problem/README
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Use `ui.navset_tabs` and `ui.nav` to put the dataframe output on a second tab.
Modify the page layout by using `with ui.navset_tab()` and `with ui.nav_panel()` to create a tabbed layout. Your app should show the plots on a tab called "Error" and the data on a tab called "Data".
72 changes: 72 additions & 0 deletions docs/exercises/express-306-navset/problem/app-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from shiny import ui, render, reactive, App
import pandas as pd
from pathlib import Path
from plots import temp_distirbution, daily_error
import shiny.experimental as x

infile = Path(__file__).parent / "weather.csv"
weather = pd.read_csv(infile)
weather["error"] = weather["observed_temp"] - weather["forecast_temp"]


card1 = x.ui.card(ui.output_plot("Plot"))
tab1 = ui.nav(
"Tab1",
card1,
ui.output_text("some_text"),
)
tab2 = ui.nav("Tab2", ui.output_data_frame("data"))
tab3 = ui.nav("Tab3", ui.output_image("image"))
ui.navset_tab(tab1, tab2, tab3)


app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_date_range("dates", "Date", start="2022-01-01", end="2022-01-30"),
ui.input_selectize(
"cities",
"Select Cities",
weather["city"].unique().tolist(),
selected="BUFFALO",
multiple=True,
),
ui.input_slider("alpha", "Plot Alpha", value=0.5, min=0, max=1),
width=3,
),
ui.panel_main(
ui.output_plot("error_distribution"),
ui.output_plot("error_by_day"),
ui.output_data_frame("data"),
),
)
)


def server(input, output, session):
@reactive.Calc
def filtered_data():
df = weather.copy()
df = df[df["city"].isin(input.cities())]
df["date"] = pd.to_datetime(df["date"])
dates = pd.to_datetime(input.dates())
df = df[(df["date"] > dates[0]) & (df["date"] <= dates[1])]
return df

@output
@render.plot
def error_distribution():
return temp_distirbution(filtered_data())

@output
@render.plot
def error_by_day():
return daily_error(filtered_data(), input.alpha())

@output
@render.data_frame
def data():
return filtered_data()


app = App(app_ui, server)
65 changes: 65 additions & 0 deletions docs/exercises/express-306-navset/problem/app-solution-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from shiny import ui, render, reactive, App
import pandas as pd
from pathlib import Path
from plots import temp_distirbution, daily_error

infile = Path(__file__).parent / "weather.csv"
weather = pd.read_csv(infile)
weather["error"] = weather["observed_temp"] - weather["forecast_temp"]

app_ui = ui.page_fluid(
ui.panel_title("Weather error"),
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_date_range("dates", "Date", start="2022-01-01", end="2022-01-30"),
ui.input_selectize(
"cities",
"Select Cities",
weather["city"].unique().tolist(),
selected="BUFFALO",
multiple=True,
),
ui.input_slider("alpha", "Plot Alpha", value=0.5, min=0, max=1),
width=3,
),
ui.panel_main(
ui.navset_tab(
ui.nav(
"Error",
ui.output_plot("error_distribution"),
ui.output_plot("error_by_day"),
),
ui.nav("Data", ui.output_data_frame("data")),
)
),
),
)


def server(input, output, session):
@reactive.Calc
def filtered_data():
df = weather.copy()
df = df[df["city"].isin(input.cities())]
df["date"] = pd.to_datetime(df["date"])
dates = pd.to_datetime(input.dates())
df = df[(df["date"] > dates[0]) & (df["date"] <= dates[1])]
return df

@output
@render.plot
def error_distribution():
return temp_distirbution(filtered_data())

@output
@render.plot
def error_by_day():
return daily_error(filtered_data(), input.alpha())

@output
@render.data_frame
def data():
return filtered_data()


app = App(app_ui, server)
Loading

0 comments on commit ab2474f

Please sign in to comment.