R Shiny is coming to Python



Original Source Here

Photo by Stephen Dawson on Unsplash

R Shiny is coming to Python

Shiny is joining the ranks of web app tools such as Streamlit and Dash

Recently RStudio announced that the company is going to change its name to Posit (meaning: to propose an idea or theory) to reflect the company’s plans to expand its focus beyond R. Their efforts will now also cover Python users and integration with Visual Studio Code. However, R users were set at rest that the company is not pivoting from R to Python and RStudio IDE is not going to be renamed. The company is just expanding its data science ecosystem.

At the same time, Posit announced the release of Shiny for Python. I am quite certain that R Shiny does not require an introduction. But in case it does, it is a package/framework that allows data scientists to build interactive web apps straight from R, without any web development knowledge.

Since its introduction 10 years ago, R Shiny was widely adopted in the community. Now, we can also use the framework in Python. Shiny for Python is currently in the alpha stage and probably a lot will change in the coming months. But we can already take it for a spin and see how it works.

In this article, we will see how to create simple Shiny apps in Python. We will not go into details of how Shiny actually works, as there are already a lot of great tutorials covering that topic on Medium. Let’s dive right into it!

Shiny for Python

As always, we need to start with installing the library. We can do so using pip:

pip install shiny

Then, we need to create a directory, for example:

mkdir python_shiny_example
cd python_shiny_example

When we are in the directory, we run the following command:

shiny create .

It generates a sample app.py file containing a simple demo app. The following snippet contains the code of the app:

To run the app, we can use the following command in the terminal:

shiny run --reload app.py

Luckily for us, Posit is making it even simpler to develop Shiny apps by offering a VS Code extension. With the extension, we can focus on developing the app without the need to leave the IDE. After installing it, we can run the app within VS Code using the play (or run) button. The following image illustrates what the demo app looks like when executed in VS Code.

Creating a simple app

We have already seen the demo app, now it is time to build something ourselves. We will build a very simple stock price tracker. I chose to use stock prices as input data, as we can easily download the data on the fly (you can read more about it here). And while doing so, we can showcase different kinds of inputs for our app.

The following snippet contains the code of our app:

A few words of comment on what actually changed compared to the demo app. First, we cover the US changes. For this app we are using a page layout — a panel with a sidebar. In the sidebar, we are asking the user to input two pieces of information:

  • the ticker of the stock of interest — we are using a drop-down menu with two possible values,
  • the date range for which we want to download the prices.

In the main panel, we are creating a simple plot of the closing price over time. In this section, we could also print the downloaded dataframe with raw price data, etc.

Within the server function, we defined a helper function that downloads the required stock prices using the inputs from the sidebar. Then, the function creates a simple time series plot using the plot method of a pandas DataFrame.

The result looks as follows:

Naturally, we can build infinitely more complex apps with Shiny. However, with this example, I wanted to show how simple it is to build something from scratch in a matter of minutes.

Thoughts

I am really curious to see the next developments of Shiny for Python and its adoption. Currently, Python’s most popular web app frameworks are Streamlit and Dash. Will Shiny be able to grab some of that user base? Time will tell.

Posit is also offering smooth integration with their Connect platform, which makes it easy to deploy the Shiny apps. This might be a big benefit that will tip the scales toward wide user adoption.

Personally, I am definitely enthusiastic about it, as I have spent quite some time building Shiny apps in R. I have also seen R Shiny being pushed to the limits in order to build a full-fledged data portal used for various data-related tasks by non-technical stakeholders. Building such a tool required quite a lot of customization and I do not think it would be simple/possible to build something that complex in Streamlit.

Fun fact: In the examples section of Shiny for Python’s website, we can find a clone of the popular browser game Wordle. I recommend you check it out, just to see how versatile Shiny can be.

Takeaways

  • Posit (former RStudio) recently released a Python version of its Shiny package,
  • Using Shiny for Python, data practitioners can create complex and interactive web apps without prior web development knowledge,
  • We can also use Shiny’s online environment to experiment with building simple apps without installing anything locally,
  • The shiny library is currently in the alpha stage, so it will still be some time before we can use it for building production-ready apps.

You can find the code used for this article on my GitHub. I am curious to hear what are your thoughts about extending Shiny for Python users! Please let me know in the comments or on Twitter.

Liked the article? Become a Medium member to continue learning by reading without limits. If you use this link to become a member, you will support me at no extra cost to you. Thanks in advance and see you around!

You might also be interested in one of the following:

References

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: