
Getting Started with Shiny for Python - in the browser! || Winston Chang || Posit
Shiny makes it easy to build interactive web applications with the power of Python’s data and scientific stack. You can try out Shiny for Python without installing a single thing… All in the browser. Learn more about Shiny for Python: https://shiny.rstudio.com/py/ Check out our interactive Shiny for Python examples: https://shinylive.io/py/examples/ Content: Winston Chang (@winston_chang)
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Today I just want to show people how to get started making a basic Shiny app and just understanding the basic concepts. In order to do that, we can just start from the website. This is the Shiny for Python website. It's shiny.rstudio.com.py. I know that might be really little in the browser, but we'll post a link for that. And the easiest way to get started just working in code is just click on examples. All right, and that will take you to this website, shinylive.io, where you can just work on your code here.
You can look at the examples, edit the code, and run the apps. This is one of the most exciting things, I think, is that you can use Shiny without having to install anything, without having to install even Python on your system. You can just get in here and start working right away. And then, you know, as you develop an app that becomes more sophisticated, you can work in a regular Python installation. But to get started, this is a great way to do it.
This is one of the most exciting things, I think, is that you can use Shiny without having to install anything, without having to install even Python on your system.
Understanding the basic structure of a Shiny app
Okay, so let's take a look at this example app. There's a whole bunch of different examples over here, but by default, it just starts on this page. And to understand how a Shiny application is built, there's two main components. There's the UI, which is the part that's displayed in the web browser. This is a web page that's displayed in the web browser over here. And then there's the server logic. And this is Python code that gets executed when somebody connects to this application. It executes this code once, and then it will re-execute reactive functions in response to user input.
So there's a UI over here. There's the server code here. And then that stuff gets put together into a Shiny app object over here. And so we're calling this app object app, which is that's what you have to call it for this all to work. Let's take a look at the UI. So in this case, we have input slider, which is this thing over here. And then output text verbatim. So this is a text output, and the verbatim means that it's printed as code with a monospace font instead of being printed as normal HTML text. And as we change the slider, it causes this output to change.
All right. So we've got this input and this output component, and they're wrapped up inside of something called a page fluid. Now, we don't have to know the details about this right now, but for most applications at this point, we can just wrap these components in a page fluid. All right. So now what's connecting them together is this server logic here. This is a function. It's called server. And as I said earlier, it gets executed once when the user connects to the application. Now, there's a bunch of these decorators here. This is saying we've got this thing that's an output. It's rendering text. And then here we're defining the function txt, which returns a string.
Renaming variables and reactive computing
Now, I'm going to change some of these names just to make it a little easier to explain things. So let's say we'll call this slider my slider and input my slider and text. You know, I'll call it txt computation. All right. We'll put this over here as well. So I made some changes to this application. I'm just going to I can click this button to rerun it. Now, it looks the same. But over here, the variables should be it'll be a little easier to see how things line up.
So I've got the slider input. And it's the idea of it is my slider. And then I can read it in this function down here. So I can say input dot my slider. And I invoke that as a function. And this this function will return the current value of the slider. And then what I'm taking what I'm doing with that value is I'm putting into a string. And this function text computation, it takes this string, and it's sticking it into this output text verbatim. One of the cool things about Shiny is its reactive computing model. So if I move this slider, I don't have to do anything extra to tell it to re execute this function, it just knows, like, hey, input my slider is changed. So re execute this function, every time I do that.
One of the cool things about Shiny is its reactive computing model. So if I move this slider, I don't have to do anything extra to tell it to re execute this function, it just knows, like, hey, input my slider is changed.
Now I can do some other things to this, like I can change this label here, I can say, you know, input a value here. And I can, because this is all running within the web browser, Python is running in the web browser, and Shiny is running in the web browser, I can just click this button here and re execute, re execute this application every time I make changes to it. So it makes it really easy to try out new things.

