1. Introduction to Shiny for Python
Shiny for Python is a powerful web application framework for building interactive data visualizations, dashboards, and applications using Python. For a while, data scientists where looking for an R Shiny alternative in Python, and now they have it. While Shiny for R is with us already since 2012, Shiny for Python is a relatively new addition, it was introduced by Posit in 2022.
Both frameworks are built to allow data scientists to create interactive web applications quickly, without the need for web development skills. All you need is understanding of R or Python, and you can build a dashboard that can be shared with stakeholders, or used for decision support.
2. Understanding Shiny for Python
Shiny for Python builds on the experience and lessons learned from Shiny for R, introducing unique features and capabilities. While its syntax and architecture are similar to R Shiny, it has distinct naming conventions and API differences.
Like R Shiny, Shiny for Python follows the reactive programming paradigm, where changes in input values trigger updates in output elements. This powerful concept allows for the creation of highly interactive and responsive applications with minimal development time. It also facilitates an easier transition for data scientists familiar with R Shiny compared to other web frameworks like Flask or Django.
Shiny for Python consists of two main components: the UI and the server. The UI component defines the application's layout and appearance, while the server component handles logic and data processing. This separation of concerns simplifies the development and maintenance of complex applications, making it more efficient than using Jupyter notebooks or Streamlit.
For a guide on creating your first Shiny app in Python, follow the Shiny for Python introduction blog post. For a more comprehensive guide, check out the Shiny for Python tutorial materials prepared for the 2024 Shinyconf workshop.
3. Why Choose Shiny for Python?
Before choosing any technology, it's important to understand the benefits and limitations of the framework. What are the highlights and the drawbacks of each technology?
In our experience, Shiny is a perfect fit for teams and projects that:
- Want to build interactive web applications quickly and validate ideas from day one.
- Need to share insights and data with stakeholders in an organized and visually appealing way.
- Require dashboards where users can take actions and make decisions.
- Have around 1-100 concurrent users.
This applies to both Shiny for R and Shiny for Python. You should consider Shiny for Python if you would like to:
- Use libraries and packages from the Python ecosystem.
- Code in Python while leveraging the existing team's Shiny for R knowledge (frameworks are very similar in terms of syntax and architecture).
- Take advantage of Python's rich tooling for reproducible environments, testing, type checking, and more.
In the next sections, we'll compare Shiny for Python to other popular web frameworks and tools directly, to help you make an informed decision.
Shiny for Python over R Shiny
If you're already familiar with R Shiny, you might be wondering why you should consider Shiny for Python. The main advantage of Shiny for Python is the ability to leverage Python's extensive libraries and tools for data analysis, machine learning, and visualization, as well as the Python ecosystem as a whole.
In some cases, you may have a project with code that uses NumPy, Pandas, PyTorch, TensorFlow, or any other Python library, and you want to build a dashboard on top of it. Then, Shiny for Python is a natural choice, a framework that strikes the sweet spot between simplicity and flexibility. Even if you don't have a Python codebase yet, you might want to consider Shiny for Python if you want to take advantage of Python's rich tooling for reproducible environments, testing, type checking, and more. For a more in-depth comparison of the Shiny frameworks, check out our guide on Shiny for Python vs. R Shiny.
An important, non-technical aspect of choosing Shiny for Python is the greater availability of Python developers. Many data science teams are split between R and Python, some are Python specific. Right now Python teams can take full control and use the reactive model familiar from R Shiny dashboards.
Shiny over Excel
Excel is a powerful tool for data analysis and visualization, but it has its limitations. One might be tempted to build a dashboard in Excel, but it's not scalable, maintainable, or secure. Excel files are known to be prone to errors and difficult to version control. Shiny solves these problems by providing a robust framework for building interactive web applications that can be easily shared, deployed, and maintained with strict version control. If you're considering switching from Excel to Shiny, check out our guide on how to switch from Excel to R Shiny.
Shiny over Streamlit
Streamlit is a popular Python library for building data apps, and it's great for super quick prototyping. However, at Appsilon, we use Streamlit only for very simple applications like PoC done in less than a day. From our experience, Streamlit's reactive programming model is far less powerful than Shiny's, and we very quickly hit the limits of what we can do with it without a lot of workarounds. Our rule of thumb is to use Shiny if we know that we will need to iterate on the app (which is almost always the case). For more information on the Streamlit vs. Shiny comparison, check out our guide on choosing the right data dashboard tool.
Shiny over Dash
Dash is a popular web application framework for building interactive web applications in Python. It doesn't have the problems of Streamlit, but writing a Dash app is more verbose than writing a Shiny app. At least to Appsilon's liking. Our experience shows that applications in Shiny are easier to maintain and develop. For more information on the differences between Shiny and Dash, check out our comparison guide. Note that it was written before the introduction of Shiny for Python.
Shiny over full-stack web frameworks
If your team is planning to create a dashboard for hundreds or thousands of concurrent users, it's a good idea to use a full-stack web framework that separates the frontend and backend. You could have a backend with Django, FastAPI, or Plumber and a frontend with React or Angular. But keep in mind, if you want to prototype and make changes quickly in this setup, you'll need a strong team of experienced software engineers who can work well together and really understand the work involved.
If you need rapid prototyping to validate your ideas and iterate quickly, Shiny is far superior. Additionally, you work with a single language, not needing to switch between Python/Go/Java and JavaScript/TypeScript.
Shiny over Jupyter notebooks
Jupyter notebooks are great for early exploratory data analysis and sharing insights between data scientists. However, they are not suitable for building production-ready applications. The reproducibility of Jupyter notebooks is a challenge, and the code is not easily maintainable. Above all, Jupyter notebooks are not designed for building interactive web applications.
A notebook can work within a scientific team, but it’s not something you should share with upper management or business teams. They will require proper user experience and natural interface for their work.
Shiny over Business Intelligence tools
Business Intelligence tools like Tableau, PowerBI, or Looker are great for visualizing data and having drill-down capabilities. Very often, those tools are supported for years. However, they have their limitations. Namely, they are not as flexible as Shiny, and creators are limited to the features provided by the tool. However, the most important issue is that you can't easily make decisions inside the dashboard. With powerful decision support systems, you can make decisions inside the dashboard. This decision can be saved in the database, pushed further to other systems and even change what users see in the tool in real time. This is a much more powerful way of working than the descriptive model which only allows looking at the data.
On top of that, with Shiny being open-source, you're free from vendor lock-in, and paid options for advanced features are optional.
Shiny over Static Reports
Tools like Quarto (RMarkdown successor) or Observable Framework are changing the way we think about reports. In the past, the report was a sad, static PDF or word file. Now, we can have a report that is a living document, with interactive elements and clean design. Under the hood, those tools are creating a static website that is very easy to deploy and scale.
For many use cases, static dashboards are a good fit. However, if you need to make decisions inside the dashboard, you should consider Shiny. The same goes for the need of writing to the database, or any other backend service. Shiny allows you to interact with all the services you want.
On the other hand, for public dashboards (like COVID-19 dashboards), static reports/dashboards are a great fit. By static, we mean that there's no backend service, however, the dashboard can be interactive and beautiful. Static dashboards are easier to deploy, scale, and maintain. While Shiny hits the scalability limit at around 100 concurrent users, static dashboards can handle unlimited users.
Our observation is that some organizations use technology that is not suitable for their use case, and they could be better with static technologies. The good news is that Shinylive allows you to convert Shiny applications into static websites, so you can have the best of both worlds. In other cases you may prefer to build an Observable Framework dashboard, or a Quarto report.
4. What Your Team Needs to Get Started with Shiny for Python
To get started with Shiny for Python, all you need is a basic understanding of Python. If you're already familiar with Shiny for R, you'll find that the transition to Shiny for Python is straightforward, as the syntax and architecture are very similar. Later in this guide, we'll provide a brief comparison of Shiny for R and for Python to help you make the transition.
The good news is that you can deploy Shiny for Python applications in the same way as R Shiny applications, via Posit Connect with the same license. Naturally, you can also deploy Shiny for Python applications on your own infrastructure, or use other cloud providers. For enterprise customers, we recommend using Posit Connect, as it provides a secure, scalable, and reliable platform for deploying Shiny applications. We invite you to check our ShinyProxy vs. Posit Connect blog post.
5. Best Practices and Tips
Below we highlight some of the most important best practices that can help your team build better applications and streamline your development process.
- Work iteratively and incrementally - Start with a simple prototype and gradually add features and complexity as needed. Validate your assumptions and get feedback from stakeholders early and often.
- Use version control - Keep your codebase clean and organized by using a version control system like Git. This will help you track changes, collaborate with team members, and roll back to previous versions if needed.
- Write clean and maintainable code - Agree upon coding standards and conventions with your team, and use tools like linters and formatters to ensure consistency. This approach not only takes the burden of maintaining the codebase style off developers' shoulders but also makes it easier to onboard new team members.
- Test and validate your code - Write unit tests and end-to-end tests to ensure that your application works as expected and catches bugs early in the development process. This saves time and money because software catches bugs, not the QA, managers, or end-users. Shiny for Python has a built-in testing framework, so you can start writing tests right away.
- Track adoption and usage - Monitor how users interact with your application. Even the most beautiful and feature-rich dashboard is useless if no one uses it. Use analytics tools to track user behavior and make data-driven decisions about future development.
6. Getting Started with Shiny for Python
To get started with Shiny for Python, you'll need to install the necessary packages and set up your development environment. For more information on how to install Shiny for Python, check out the official installation guide and our introduction to Shiny for Python blog post.
R Shiny and Shiny for Python Comparison
Assuming you're already familiar with R Shiny, you might be wondering how to transition to Shiny for Python. This short snippet will help you understand the differences between the two frameworks and get you smoothly started with Shiny for Python.
from shiny import Inputs, Outputs, Session, ui, render, App
app = ui.page_fluid(
ui.panel_title("My First Shiny App"),
ui.input_text("name", label="Enter your name:"),
ui.output_text("greeting"),
)
def server(inputs: Inputs, outputs: Outputs, session: Session):
@render.text
def greeting():
return f"Hello, {inputs.name()}!"
app = App(app, server)
The highlights:
- Code is written in Python, but the structure is very similar to R Shiny.
- The imports system of specific elements from shiny is builtin.
- The ui and render namespaces are used to define the UI and server logic.
- The @render.text decorator is used to define the output instead of output$greeting <- renderText(...).
- The naming conventions have been changed to be more pythonic, e.g., ui.input_text instead of textInput.
- All the inputs and outputs are gathered under the ui namespace, which makes it easier to find them.
Those changes might be tiny, but make a huge difference for the developer experience. We invite you to check our R Shiny Developer's Thoughts on Shiny for Python blog post to see how the R Shiny developers feel about the transition to Shiny for Python.
7. Advanced Shiny for Python Concepts
Without going into too much detail, we want to redirect you to the blog posts that cover the advanced topics in Shiny for Python.
- Shiny Modules - reusable UI and server elements that can be defined once and used multiple times in your application. Build once, use many times. our guide on shiny modules in Python
- Code Organization - Since Shiny for Python doesn't allow using output$plot <- renderPlot(...), you need to organize your code in a different way. Case study of dynamic plot management
- Custom Components - Building the advanced Shiny for Python components that require JavaScript follows the same principles as in Shiny for R. Custom Components
- Routing - Routing is a powerful feature that allows you to create multi-page applications with Shiny for Python. guide on routing
- Styling with SCSS - While styling Shiny for Python applications is straightforward using the built-in CSS classes and themes, you can take it to the next level by using SCSS (Sassy CSS). guide on SCSS
- Custom tables with Great Tables - The well-known gt package for tables is available in Shiny for Python. guide on Great Tables
- Custom testing with Playwright - While Shiny for Python has a built-in testing framework, you can make framework-agnostic tests with Playwright. guide on testing with Playwright
8. Shiny for Python Ecosystem
In the sections above, we mentioned the rich Python ecosystem several times. But what does it mean for you, your team, and your projects?
Cross-pollination of Tools
While the R ecosystem is centered around data analysis and visualization, the Python ecosystem is much broader. This means you can leverage advances not only in data science but also in web development, machine learning, and more. These tools are used by millions of developers, ensuring they are well-tested and maintained. For example, the battle-tested FastAPI (Python's equivalent of Plumber) was downloaded 79 million times last month, compared to Plumber's 116k downloads—a difference of over 680-fold!
Since FastAPI depends on Pydantic, you can use it for data validation in your Shiny for Python applications at no extra cost. There are many more examples of such cross-pollination between tools.
Development Tools
While clients may not be interested in the tools you use, they care about the quality of the product, which is directly related to the tools you use. Additionally, the developer's experience is crucial as it impacts morale and productivity.
Here are some tools that can be game-changers for your team:
- uv and Poetry for dependency management: Even with recent improvements in R with binary packages, Python is still ahead in this area. It's much easier to write interoperable code with Python if you use Poetry or uv.
- ruff for linting and code formatting: ruff is a powerful tool that helps maintain a consistent code style across your projects, similar to lintr in R but with more features.
- pytest for testing: Taking advantage of cross-pollination, pytest is the backbone of many Python projects, well-maintained, and has a rich ecosystem of plugins.
- playwright for testing: While shinytest is great for testing R Shiny applications, you often need additional tests in tools like Cypress (which we use in Rhino). With Playwright, you can write and test applications in Python, enhancing the testing experience. Playwright has a plugin for pytest, consolidating all your tests in one place.
- package indexes: Your organization might already have an artifact manager like Nexus or Artifactory. With Python, you can use it out of the box.
9. Tapyr: Appsilon's Template for Shiny Python Applications
Setting up new projects can be time-consuming and error-prone. To streamline the process, Appsilon has developed Tapyr, an open-source template for Shiny for Python applications. It's a younger sibling of Rhino for R Shiny.
Tapyr comes with a pre-configured:
- Repository structure - to keep your code organized and maintainable.
- Testing setup - to ensure you can validate your application's behavior.
- Code formatting - to maintain a consistent code style across your projects.
- Linting - to catch errors and enforce coding standards.
- Type checking - to catch errors early and improve code quality.
- Devcontainers - to ensure a consistent development environment across your team.
- CI/CD pipeline - to automate testing.
Tapyr is a great starting point for your Shiny for Python projects, allowing you to focus on building your application instead of setting up the development environment. For more information, check out the Tapyr documentation.
If you're interested in learning more about Tapyr, check out the following resources:
10. Shiny Express: Simplifying App Development
Since we're discussing Shiny for Python, we must mention Shiny Express. So far, we've focused on Shiny Core, which is the standard way to build Shiny for Python applications, similar to Shiny for R.
However, Shiny Express offers a simplified development process unique to Shiny for Python. It allows you to build Shiny applications in a more declarative manner, akin to Streamlit. This approach retains all the tooling and best practices but accelerates the development process. For more information, check out our Shiny Express introductory blog post.
11. Shinylive: Running Shiny Apps in the Browser
Another unique feature of Shiny for Python is Shinylive, which allows running Shiny applications directly in the browser without a server. This means some Shiny applications can be converted into static websites using HTML, CSS, and JavaScript, thanks to WebAssembly.
Shinylive has been available for Python for some time. Check out our introductory blog post to learn more. Recently, it was also introduced for R Shiny, and you can find the documentation here.
For simpler dashboards, Shinylive can be a game-changer, allowing deployment on static website hosting like GitHub Pages or AWS S3 without a server. While not all Python packages are supported yet, it's worth keeping an eye on this technology. Note that without a server, backend services like database writing are not possible.
12. Deployment and Hosting
After building your Shiny for Python application, you'll need to deploy it. For enterprise customers, we recommend Posit Connect for its secure, scalable, and reliable platform, which includes features like user authentication and monitoring. Learn more about Posit Connect vs. ShinyProxy.
You can also deploy on your own infrastructure or cloud providers like AWS, Google Cloud, or Azure. This requires more setup and maintenance. Start by containerizing your application with Docker and then deploy it. Check out our guide on containerizing Shiny for Python applications for more details.
For applications with a large user base, consider the possibility of turning your Shiny for Python application into a Shinylive app for deployment on static hosting. If Shinylive isn't suitable, you may need a different technology stack. At Appsilon, we can help you choose the right stack for your project.
13. Real-world Examples and Case Studies
We've covered the basics and advanced concepts of Shiny for Python. Now, let's look at real-world examples and case studies. We've rebuilt some of our R Shiny applications in Python, such as the Respiratory Disease applications: Shiny for R version and Shiny for Python version.
Despite being a new technology, Shiny for Python has already been used successfully for client projects. One such case study is described in the Cutting Bacterial Analysis Time from 7 Days to Under an Hour blog post. This example shows how Python libraries were better suited for the task, allowing quicker delivery of the solution.
14. Future of Shiny for Python
Python is the dominant language in data science, and its ecosystem is continually evolving. More data scientists are transitioning to Python, with key packages like Statsmodels and ScanPy under active development. Statsmodels bring the power of R's lm, glm, and statistical tests to Python, while ScanPy is a powerful tool for single-cell RNA sequencing analysis that is alternative to R's Seurat. With less and less key packages exclusive to R, the transition to Python is easier and easier.
Shiny for Python is ideal for fast-prototyping, production-ready, customizable decision support systems. We believe it will continue to grow in popularity, and we're excited about its future.
At Appsilon, we've been developing Shiny for R applications since 2013. With Python's rise in data science, there was a demand for a Shiny-like framework leveraging Python's libraries and tools. We've been working with Shiny for Python since its inception and are excited to share our knowledge and experience with the community.
15. Additional Resources