
Hello, World! A Quick Tour of Shiny for Python || Carson Sievert || Posit
Shiny makes it easy to build interactive web applications with the power of Python’s data and scientific stack. 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: Carson Sievert (@cpsievert) Producer: Jesse Mostipak (@kierisi) Editing and Motion Design: Tony Pelleriti (@TonyPelleriti)
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
So Shiny for Python, if you're not already familiar with Shiny, Shiny is a framework for building interactive web applications. So it's been almost 10 years to the date that we've had Shiny in R. But over the past year or so, we've been working on essentially porting that foundation over to Python so that, you know, Python users can have a similar, very approachable framework for building interactive web applications without needing to already know HTML, CSS, and JavaScript.
So even though we try to make this approachable, we still sort of build this on a foundation that's very flexible and scalable. So these are kind of built on foundations that allow you to, you know, build out your own custom components, sort of extend some of the reactivity concepts to, you know, implement your own sort of modular, you know, functionalities. And also, you know, the sort of reactive framework that powers all of this. One of the reasons why it's so nice is it kind of allows you to declare, you know, what should happen. And then Shiny handles the details of, you know, what actual computations to execute when they need to be executed. So it kind of allows you to write this expressive reactive code without needing to handle like every specific case of when this changes, then also update this other thing.
So it kind of allows you to write this expressive reactive code without needing to handle like every specific case of when this changes, then also update this other thing.
Hello world example
So for a very sort of hello world example, you can see it here running in the browser. And this is how you would start programming a very basic Shiny application. So you can see I'm importing from Shiny, there's a UI package inside of Shiny. So this is how you'll actually create or define where things should go in the actual user interface of the Shiny application. There's also a rendering package. And this is sort of utilities for rendering things like matplotlib plots or various different types of Python output into actual stuff that can go into a user interface.
And then every Shiny application is going to want this capital A app to actually bring the two main pieces of the Shiny app together, that being the user interface here from lines three to six. This is the definition of what you see on the right here, as well as the server logic, which kind of takes Python objects and renders them into things that can go into the user interface.
So in this particular basic example, I have created this user interface by first creating sort of like a page container. So we have several different variants on different types of pages that you can create. Page fluid in a lot of cases is, you know, it's pretty basic, but covers most use cases for just starting a new user interface. And then inside of there, I will put a slider components and give this an ID of lowercase n. And then this is a label that should appear just above the slider. So I'm going to put a capital N for label, define the minimum and the maximum for the slider, and then a starting value for that slider.
And the way that Shiny works is, you know, you basically in this user interface, you give every kind of user interface component an ID, and then you can reference whatever the current value of that input is on the server by saying input dot n, and then you kind of read that value by calling it like a function. So if I had several different input components with different IDs, I would, you know, of course, change the name here after the dots and then call it like a function to basically read whatever the current value is.
And you might have noticed in this server definition, when you define this server function, it takes three arguments, the input values that are coming in from this user interface, the output argument, and we're going to use this output object to basically hook up values that we create on the server up with the user interface. And then also a session which we're not going to get into here, but this can allow you to do things specific to a specific user's session experience.
All right, so with this server definition, I'm going to create an output that's going to be connected to this output text verbatim with an ID of text. And with every output, when you first create it, you're going to want to then provide a rendering decorator. So here, the at render dot text basically means I want to define a function that's going to return a string that I can just put verbatim into this whatever UI component I'm hooking up this return value to. And this part right here, the actual name of this function that I'm providing to the render text and the output decorator is going to define the output ID essentially that should match this sort of UI container that I want to hook up this value to.
Now that I've kind of declared that there's an output that I want to render as text with an ID of text or TXT and provide it inside of and provide this sort of function expression that reads a reactive value, this input dot n. By declaring it this way, then Shiny knows basically whenever this input n changes, I will re-execute this function and basically create a new return value for this text verbatim here. So that's a long way of saying like whenever I change this slider value here, this gray text box below is going to update to be two times whatever the value of this slider is.
Running Shiny in the browser
A neat thing about this that I don't really want to steal my colleague Winston's thunder too much on is the fact that this is all running in the browser. So we've kind of built this foundation for allowing you to take, you know, simple Shiny applications and run them purely in the browser without a Python process running in the background. So this is actually statically hosted on Netlify through GitHub pages. And I have a running Shiny application here that I can actually modify the source code of this Shiny application and say, I want maybe five times the current value of this slider. Restart. It's going to start with an initial value of 40 here, and now I've got five times 40 in the output.
So that's just kind of a neat side note that you'll notice as I'm kind of going through more examples. Some of these will just run purely in the browser, but you could learn more about that in another video. Currently, I don't know if this link will change at any point. I would imagine that it will eventually have the company's branding and everything in the link name. But for now, yes, pyshiny.netlify.app is the best place to go to. And then there's kind of some getting started stuff, some information on how to install, information about how to deploy to our different like professional products, going into some concepts in depth. And then there's a link here to examples that is kind of a different site with all sorts of different examples that are running in the browser.

