Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/apps/TemplateMatplotlib/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Quick example for a simple matplotlib app. Matplotlib is a popular Python librar
## Simplest matplotlib plot
The following example demonstrates how to create a simple static plot using Matplotlib within a Shiny app.
{{embed_app("100%", "500px", "static")}}
{{embed_code("static")}}
This is the most straightforward way to integrate plots into your tools by leveraging matplotlib's vast capabilities for visualizations.
## Animations using matplotlib
While it is in principle possible to create animations using Matplotlib in Shiny for Python, it requires some additional setup compared to static plots. The following example demonstrates how to create an animated plot using Matplotlib within a Shiny app.
{{embed_app("100%", "500px", "animated")}}
{{embed_code("animated")}}
This approach might lead to flickering in certain browsers, as the entire plot is re-rendered for each frame of the animation. For smoother animations it is recommended to instead use plotly (see [here](https://physicsapps.github.io/teaching/plotly-template.html) for the corresponding template), which integrate more seamlessly with web applications.
4 changes: 4 additions & 0 deletions docs/apps/TemplatePlotly/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ Plotly is a powerful graphing library that makes interactive, publication-qualit
## Simplest plotly plot
The following example demonstrates how to create a simple static plot using Plotly within a Shiny app
{{embed_app("100%", "550px", "static")}}
{{embed_code("static")}}
## Animations using plotly (python)
Plotly makes it easy to create animated plots with minimal setup. The following example demonstrates how to create an animated plot using Plotly within a Shiny app.
{{embed_app("100%", "550px", "animated")}}
{{embed_code("animated")}}
Note that this approach requires animations that loop smoothly. The entire frame stack must be preloaded when the plot is first rendered, which may lead to longer initial load times for complex animations. Additionally, this precludes a true "live" animation that updates based on real-time or on-the-fly generated data.
## Animations using plotly (injecting JavaScript)
For more advanced use cases, you can inject custom JavaScript code to control the animation behavior in Plotly directly. This allows for greater flexibility and smoother animations. The following example (from [plotly.js](https://plotly.com/javascript/animations/#animating-many-frames-quickly)) demonstrates how to create an animated plot using Plotly with custom JavaScript within a Shiny app, allowing for on-the-fly generation of new frames.
{{embed_app("100%", "550px", "animated_js")}}
{{embed_code("animated_js")}}
Y
2 changes: 2 additions & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ Quick example for a simple matplotlib app. Matplotlib is a popular Python librar
This is a simple example to showcase different matplotlib apps using Shiny for Python.
## Simplest matplotlib plot
{{embed_app("100%", "500px", "static")}}
{{embed_code("static")}}
## Animations using matplotlib
While it is in principle possible to create animations using Matplotlib in Shiny for Python, it requires some additional setup compared to static plots. The following example demonstrates how to create an animated plot using Matplotlib within a Shiny app.
{{embed_app("100%", "500px", "animated")}}
{{embed_code("animated")}}
This approach might lead to flickering in certain browsers, as the entire plot is re-rendered for each frame of the animation. For smoother animations, consider using libraries specifically designed for interactive visualizations, such as Plotly or Bokeh, which integrate more seamlessly with web applications.
```
This documentation differs slightly from standard markdown files, as it includes a few additional necessary sections.
Expand Down
5 changes: 5 additions & 0 deletions gen_shinylive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
from pymdownx.slugs import slugify
import platform
import shutil

target_url = 'https://physicsapps.github.io/teaching/' #use this for QR-code generation via e.g. https://pypi.org/project/qrcode/


Expand Down Expand Up @@ -44,3 +46,6 @@

os.makedirs(Path("./site", target_shinypath), exist_ok=True)
_export.export(target_apppath, Path("./site"), subdir=target_shinypath, verbose=True, full_shinylive=True)

# Copy source code incase the embed_code macro is used
shutil.copyfile(app_path, Path("./site", target_shinypath, "app.py"))
21 changes: 16 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pathlib import Path
import platform

def get_app_link(current_path, subdir: str):
def get_app_link(current_path, subdir: str, target: str):
parts = current_path.parts
html_filename = str(parts[-1]).split('.')[0]
if subdir is not None:
html_filename += '_' + subdir

if platform.system() == "Windows":
app_link = str(Path('apps_' + html_filename, 'index.html'))
app_link = str(Path('apps_' + html_filename, target))
elif platform.system() == "Linux":
app_link = str(Path('apps', html_filename, 'index.html'))
app_link = str(Path('apps', html_filename, target))
else:
raise ValueError("Unsupported operating system. This script only supports Windows and Linux.")
return app_link
Expand All @@ -23,9 +23,20 @@ def define_env(env):
@env.macro
def embed_app(width='100%', height='600px', subdir: str = None):
current_path = Path(str(getattr(env, 'page'))) # " /blog/plotly-penguins-app.html"
return f"<div>\n<iframe src={get_app_link(current_path, subdir)} width={width} height={height}></iframe>\n</div>"
return f"<div>\n<iframe src={get_app_link(current_path, subdir, "index.html")} width={width} height={height}></iframe>\n</div>"

@env.macro
def embed_code(subdir: str = None, language: str = 'python'):
current_path = Path(str(getattr(env, 'page'))) # " /blog/plotly-penguins-app.html"
if language == 'python':
with open(Path("site", get_app_link(current_path, subdir, "app.py"))) as f:
code_text = f.read()
embed_text = "\n``` py title='app.py' linenums='1'\n" + code_text + "\n```\n"
else:
raise KeyError("Unsupported language")
return embed_text

@env.macro
def doc_env():
"Document the environment"
return {name: getattr(env, name) for name in dir(env) if not name.startswith('_')}
return {name: getattr(env, name) for name in dir(env) if not name.startswith('_')}
Loading