3MW (R-Shiny teaches you Py-Shiny)

Guten Tag!

Many greetings from Munich, Germany. I’ve recently built a small Shiny app to generate images via prompt inputs. But the thing is: I’ve had to use Python because Python has all the cool generative AI stuff. So, let’s talk about Shiny for Python.

Don’t worry if you don’t know Python! Part of the point I’m trying to make is that you can easily pick up Py-Shiny if you already know the R version. But before we do that, time for my regular announcements.

Course progess

I’m almost done with the code for the charts that we’re going to create in Part 2. My plan is to record all of the corresponding videos before Christmas and do a full course release by then 🎉.

In the meantime, people who participate in the pre-sale seem to enjoy the content that’s already there. For example, here’s a nice five-star review I got today.

If you also want to join the course, you can do that on the course website.

Now, let’s dive into this weeks newsletter. As always, all of the code is available on GitHub.

A skeleton first

Let’s start with an app skeleton. In R, that’s just an empty UI and an empty server.

In Python, it’s the exact same thing. The syntax is just a little bit different. We have to import the things from Shiny that we want to use with import one-by-one. For now, that’s only App, ui and run_app (that list will grow though so keep that in mind)

In Python, functions are defined with def <fct_name>(<arguments>). So really what we do here is the same as in R:

We define a function called server and use a fluid page to create a UI. Afterwards, we wrap both things into a function that turns these things into an app. The only difference is that in Py-Shiny we need another function to actually start the app.

Our first UI elements

Now the cool thing with Py-Shiny is that we can access all UI elements via ui. And even better all input functions start with input_.

As you can see, all the inputs that you know from R-Shiny are there. And they use the same naming convention of “first ID, second label, afterwards input specific stuff”. So nothing new you have to learn here. So let’s put this to a test and throw in a couple of UI elements.

This will give us a functioning, yet boring UI that looks like this:

Notice that our UI code also includes an output. That’s another cool thing with the Py-Shiny naming conventions: All outputs start with output_.

Sidebar layout

Our UI is very boring. So let’s add a little bit to it. For example, R-Shiny intros are nothing without sidebar layouts. Let’s use that in Py-Shiny too.

Sure the app will not be super beautiful. But the sidebar in Py-Shiny is collapsible by default. That’s cool.

Adding some reactivity

Alright, enough with the UI. Let’s add some server-side logic. It’s probably best to just show you an example of a how the server function could render the text output. So let’s have a look and then I’ll explain.

Without fully understanding the Python magic of how this works, we can make sense of this. First, notice that we define a new function text_output(). The name of the function corresponds to the ID of the text output. Coincidence? I don’t think so.

Second, this function returns a text that is a concatenated (+) string consisting out of

  • input.text_input(),

  • a white space and

  • input.numeric_input() (that was transformed to a string via str().)

Notice that text_input and numeric_input are the IDs of the inputs that we have specified in the UI. So, this seems to get the inputs and concatenate the strings. Basically, it’s the exact same thing as doing paste0(input$text_input, ' ', input$numeric_input) in R.

Finally, the newly defined function text_output is rounded off with so-called decorators @output and @render.text. I might at some point think and write about what these decorators are from a programming perspective.

But for now. let’s just accept that these need to be there to decorate our text_output() function. At the end of the day, this just seems to be the Python equivalent of R’s output$ID <- renderText(...). In any case, here’s the resulting app:

Nice, we have successfully built a Py-Shiny app by translating our R-Shiny knowledge to the Python world. These are baby steps but if you don’t know much about Python, this might already be a lot to take in.

So let’s end this here for today and next week we resume this journey. As always, if you want to reach out to me, just reply to this mail or find me on LinkedIn.

See you next week,
Albert 👋

If you like my content, you may also enjoy these:

Reply

or to participate.