Join the Shiny Community every month at Shiny Gatherings

Appsilon's PyShiny demo (Python for Shiny example) blog banner

PyShiny Demo: An R Shiny Developer’s thoughts on Shiny for Python


Appsilon prides itself on building the best Shiny dashboards. So naturally, when RStudio (Posit) announced Shiny for Python (PyShiny) we got very excited. And of course, we had to try it out for ourselves with a Shiny for Python demo. We’ll cover our initial thoughts, but if you want the tl;dr: PyShiny is a great addition for Python fans and you can make a decent dashboard, but it still needs a lot of improvement to be on par with R Shiny. R Shiny developers can sleep peacefully for another 6-12 months.

Did you know RStudio is rebranding to Posit? See why we think that’s a good thing and what it means for the R community.

Python is a great programming language for both Data Science and Software Development. As Shiny (and RStudio) move to combine these two worlds, PyShiny sounds like a perfect match.

After the announcement, Appsilon had a team of three developers test PyShiny. Because we know the R/Shiny realm quite well, it seemed natural to challenge the new framework by trying to port one of our R Shiny apps built during an app sprint – {Respiratory Disease}.

We wanted to see what problems we would face along the way using the alpha version of Shiny for Python. You can explore the result of our experiment and read our commentary on our first impressions below.

Table of Contents:


The Good in PyShiny

PyShiny backend

First, I would like to highlight that Shiny for Python relies on uvicorn web server as its backend. You might have heard of uvicorn before; it powers one of Python’s most popular backend frameworks and in Web Development in general – FastAPI. It is very robust and performant, so there is no doubt that PyShiny applications will be fast and scalable if built right. 

univcorn powers PyShiny backend

PyShiny is appealing for software development

As a Software Developer, I noticed a few improvements that we get “for free” from Python itself. Like type hints. It’s not a strongly typed approach like in TypeScript, but it still allows developers to catch some bugs at build-time, rather than run-time.

PyShiny type hints and code suggestions

Not to mention that even advanced R Shiny developers need to open the Shiny reference page from time to time to check what methods and properties exist in the `session` object. In Shiny for Python, you can see that immediately in the IDE.

IDE’s for PyShiny

Speaking of IDE – Visual Studio Code is the recommended tool for writing Shiny for Python applications. Shoutout to Shiny Team for creating a special VSCode extension! This is a great choice since VSCode is a multilingual code editor. It makes it easy to write Python, JavaScript, CSS, etc in one project using a single code editor.

Quarto, Python, and VS Code? Level up your reporting with Quarto Reports in VS Code!

Bonus features of PyShiny

Another cool VSCode feature that is supported in Python and also in Shiny for Python, is the ability to see not only the definition of a user-defined function but also the references – places in the code, where this function is called.

PyShiny user defined function and references

  • Async/await out of the box. Even `session.send_custom_message()` is implemented as an asynchronous function

PyShiny Async/Wait

  • Hierarchical project structure and modularity (something that Rhino achieves by leveraging `box` package), 

PyShiny Modularity

  • Naming conventions. `snake_case` for functions and variables, and `PascalCase` for classes. Also, you will notice immediately that instead of familiar `reactive` and `observe`, PyShiny now has `reactive.Calc` and `reactive.Effect` – which makes a lot of sense. Even Joe Cheng gave credit to this during the Panel Discussion during the 2022 Appsilon conference.
  • Trailing commas inside UI components. It might sound minor, but this is a nice bonus for those reading git diff regularly. On the screenshot below, notice how adding `style` parameter results only in one line being changed:

PyShiny trialing commas

The Bad in PyShiny

Lack of packages

Shiny is a great framework for developing web applications in R because of the ecosystem of related packages: development tools, charts, tables, UI components, etc. At this stage, Shiny for Python lacks such an ecosystem.

While Shiny developers have access to such great component libraries like shiny.semantic or shinyWidgets, in Python everything has to be written from scratch. Of course, we could implement almost anything using JavaScript to directly access the APIs of some popular visualization or component libraries, but this would dissolve the very spirit of Shiny – simplicity, and accessibility for everyone.

The Shiny for Python ecosystem might be lacking, but R is booming. Build better Shiny apps the Appsilon way with Rhino.

Kudos to the Shiny Team for their efforts to leverage the existing ecosystem of ipywidgets with the help of shinywidgets – so that Shiny for Python users/developers have something to work with. However, we tested ipyleaflet which was featured in one of the Shiny for Python demos, and we struggled with it. Its R counterpart (leaflet) feels more flexible, well-documented, and feature-rich.

Unfamiliar implementations

Implementation of some familiar Shiny features is different in Shiny for Python. For example, shiny.ui.tags.head – it’s not the <head />. This will still work if you need to reference a local JS or CSS file, but in some cases, it’s crucial to add something to the head tag (e.g. the PWA worker). In some cases, things could be broken in unexpected ways: when created inside a UI module, selectize input with multiple selection would not render or work properly. 

At the time of code writing, the issue above was already fixed in the development version on Github. But it’s an important reminder that Shiny for Python is still in its infancy.

Running hot

Last but not least, something’s off with how PyShiny runs the application – starting the app makes the CPU immediately hot. This behavior was observed both on a Linux and a macOS machine.

Let us know in the comments below if you found the same issue.

The Beauty of PyShiny

ShinyLive

During the rstudio::conf(2022) RStudio also announced ShinyLive [https://shiny.rstudio.com/py/docs/shinylive.html] – Python code compiled to WebAssembly (WASM) which runs entirely in the browser! It is a new and exciting thing that could become a killer (cool) feature of Shiny for Python. 

Why is this exciting? First, WASM allows a serverless architecture (not serverless serverless, but the true absence of a server!). The entire app bundle is downloaded into the client, and everything happens in a browser. Another great feature of WASM applications is that once downloaded, they can be run completely offline. It’s not always straightforward to make a ShinyLive out of a PyShiny app, but we were able to do it!

This technology still has its limitations though. Packages used in such Python projects are limited. For example, we had problems with packages that are not written in pure Python. It’s also noteworthy that the bundle that needs to be downloaded is quite large, which negatively affects the start time of the application.

PWA – Progressive Web App

There is already a solution for R Shiny apps to be downloaded as a PWA. The good news is that we were able to make it work with a PyShiny app as well. It was not trivial though and required some extra effort, but the result is worth it.

We look forward to combining ShinyLive and PWA technologies, to get an offline client app for a mobile device. Think of Shiny Native for all you React fans out there.

Summing up PyShiny – an unfinished story

The problems described above are not surprising; Afterall, Shiny for Python is still in the alpha stage. The package is being rapidly developed by the RStudio Team and some issues are being fixed on the fly. And as more people test Shiny for Python, we’re bound to see quick progress. 

Python Dash or R Shiny? See which you should choose for your use case.

Personally, it’s clear that these problems are only temporary. The ecosystem of packages will eventually grow around Shiny for Python – just as with the early days of Shiny for R. The Appsilon team is excited at the chance to contribute to its success. And we look forward to adding to the PyShiny ecosystem. 

In case you didn’t notice, we’ve been using PyShiny and Shiny for Python interchangeably. So what is the official name of RStudio’s Shiny version for Python?

The official name is “Shiny for Python.” However, some folks are already using the unofficial, condensed version: Py Shiny or PyShiny.

What’s the official name of R Shiny? The official name for RStudio’s Shiny for R is “Shiny.” However, this to began to change with the community (somewhat clairvoyantly) dubbing it R Shiny or R/Shiny.

Want to get started with Shiny for Python? Check out our tutorial introducing PyShiny.

Moving forward, these unofficial names might actually be easier to distinguish in conversation which language is being used for a given project. What do you think?