
How to deploy a Dash application from VS Code to Posit Connect
Episode 1: Publishing a Dash application to Posit Connect Led by: Ryan Johnson, Data Science Advisor Follow-up links: * Posit Team: https://posit.co/products/enterprise/team/ * Talk to us directly: https://posit.co/schedule-a-call/?booking_calendar__c=RST_YT_Demo * Follow-along blog post: https://posit.co/blog/deploying-a-dash-application-to-posit-connect/ * Source code for example: https://github.com/sol-eng/python-examples * Posit Team demo resources: pos.it/demo-resources Timestamps: 00:53 - What is Posit Team? (Posit Workbench, Posit Connect, Posit Package Manager) 3:25 - JumpStart Examples in Posit Connect 4:00 - Opening up a VS Code Session in Posit Workbench 8:03 - Downloading the Dash JumpStart example in Posit Connect 9:40 - Open up the JumpStart example in VS Code in Posit Workbench 11:18 - Install the required Python packages using pip 12:55 - Python packages on Posit Package Manager 15:12 - Defining the packages you want to install (latest, specific date, etc.) 19:12 - Creating an API key in Posit Connect 20:29 - Deployment process from VS Code to Posit Connect 21:36 - Dash application hosted on Posit Connect 22:23 - Access controls in Posit Connect (sharing with the people who need to see your app) 24:10 - Adjusting the runtime of an application in Posit Connect (controlling compute resources) 26:35 - Tags in Posit Connect 26:48 - Vars (if your content requires privileged access) 27:15 - Recap of what was covered in the demo On the last Wednesday of every month, we host a Posit Team demo and Q&A session that is open to all. You can use this to add the event to your own calendar. Who are these monthly demos for? Everyone is welcome to join us - regardless of industry, background, or experience! We will discuss topics that will speak to: * Data scientists and administrators new to Posit Team or are looking to grow their understanding of our toolchain, * Teams searching for a new analytic platform built to support open-source data science, * And, those that are just curious about Posit Team! What you can expect from the monthly Posit Team demo: During the session, we will walk through an end-to-end data science workflow and demo the core functionality of Posit Team while highlighting some of our latest features! While each session's content will vary slightly, here are a few core topics we will address each month: * Open Source Analytics: The future of data science is open source. We'll discuss methods for leveraging open-source tools and packages in a secure and scalable way! * Deployment: How to share the amazing data science assets your Team has built, including web applications, machine learning models, APIs, and more! * Data Access: Data comes in various forms and is stored in various ways. We'll discuss best practices for accessing, reading, and writing data! * Job Scheduling: Do you have recurring data science jobs? We'll show you how to automate these processes using Posit Connect. What is Posit Team? Posit Team is a bundle of our popular professional software (Posit Workbench, Posit Connect, and Posit Package Manager) for developing data science projects, publishing data products, and managing packages. Registration is not required. The event will be streamed through YouTube Premiere
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Hello, everybody. My name is Ryan Johnson, and I'm a data science advisor here at Posit. And welcome to our first-ever enterprise community meetup, where we'll discuss an end-to-end data science workflow using Posit Team.
So this will be a recurring event. We'll probably have it the last Wednesday of every month. And we hope it'll serve as a good primer for those that are new to Posit Team, but also help current Posit Team users become more familiar with our tools. And every month, we'll cover a different topic, and we'll highlight all three tools within Posit Team. So Posit Workbench, Posit Connect, and Posit Package Manager.
And then after each presentation, we'll have a casual discussion where folks can ask us questions, Posit questions, or we can just discuss workflows with other community members. So with that, let's go ahead and get started.
What is Posit Team?
And I first want to start and make sure that everyone on the call is familiar with Posit Team from a high level. So this is Posit Team. We're going to start up here at the top with the data scientists, the data analysts, the folks that are actually writing code. And many times their job is to create insights and to share those insights.
So let's start by going to the left here and creating insights. They're going to be writing code. They can use R, Python. They can use whatever IDE they want, either RStudio, JupyterLab, Jupyter Notebooks, VS Code, all within Posit Workbench. So Posit Workbench is our server-based implementation of the various IDEs. And your data scientists are writing code, again, in whatever language they want.
So if you're within R, they might be creating things like Shiny applications, pins, R Markdown, Plumber. But if they're Python developers, they might be creating things like Flask or FastAPIs, Streamlit, Dash, Bokeh applications, Jupyter Notebooks. Maybe they're also creating Quarto documents, which is one of our newest tools.
So when they create content like this, it doesn't do anyone any good if you can't share it with the people that need to see it. And that's the whole purpose of Posit Connect, our second tool within Posit Team. This is our professional publishing platform. And this makes it super easy to share all the cool content you see here with the people that need to see it. Like your boss, decision makers, maybe yourself, maybe your friends and family. You can share it with Posit Connect.
So for today's session, we're primarily going to live within Posit Workbench and Posit Connect. But there's also one other tool we'll touch upon today, and that's Posit Package Manager. And it does pretty much as its name implies. It helps control all those great open source packages within R and Python. Also, any, you know, packages you've created yourself within Workbench and Connect. And so we'll talk about all three of these tools today that, again, round out Posit Team.
JumpStart example: the Dash stock dashboard
Now, for today's session, we are actually going to live within VS Code. So we're going to go into Posit Workbench and we're going to open up a VS Code session. And we're going to go through an example where we take a Dash application. It's actually a dashboard. We're going to publish it to Posit Connect and share it with everyone here on the line. So we're going to primarily live within Workbench and Connect today. And then we'll touch upon Package Manager.
So this Dash application is actually built into our jumpstart examples you'll find when you first log into Posit Connect. So when you log in, you can click on R or Python. So you have Python selected and you can see a variety of other examples. But we're going to create this stock pricing dashboard, which is a Dash application.
And this is just a screenshot of what some of that content looks like within that Dash application. So again, we're looking at stock prices. We'll have a few different stocks you can choose from. And we'll get the chance to explore this application once we actually deploy it. So with that, let's go ahead and get started.
Opening VS Code in Posit Workbench
So I'm going to be leveraging our demo server here at Posit. We have our three professional tools right here. And let's go ahead and start within Posit Workbench. So I'm going to go ahead and open this up in a new tab.
And when you first log into Posit Workbench, this is the screen you're going to see. On the right-hand side, I have various RStudio projects that I've been working on. And I can easily click on one of them and jump right back in. Or I can start up a brand new session. So let's go ahead and do that.
And this, again, is one of the best parts about Posit Workbench, is that no matter what your data science team uses, their preferred IDE, the hope is that we can support it here within Posit Workbench. So we currently support Jupyter Notebook, JupyterLab. We have the RStudio IDE as well. And then we have the VS Code Editor, which is a great all-purpose editor, which is what we're going to use today. So I'm going to click on VS Code.
And when we do that, we get some additional options. So I can give this VS Code session a name. I'll just leave it as a default right here. And then right underneath, you're going to see Cluster Kubernetes. Now, to tell you a little bit more about our demo environment here at Posit, it is all hosted in AWS, Amazon Web Services. And it's in a high-performance computing cluster, an HPC environment. And that environment is managed using something known as Kubernetes. Some of you may have heard of Kubernetes. That's one of these HPC managers that we support.
If you also use Slurm, we can support that as well. Or you can also just run these tools on a single server. That's totally fine as well. But one of the advantages of using Kubernetes or Slurm is you can actually control this job's CPU and memory allocations. So if you just have like a small job, you want to maybe create a plot or a simple application, you can keep the CPU low, keep the memory low. And that's just great for managing compute resources.
But if you have a higher compute or more demanding job, I can bump that up, for example. I can change the memory as well. So really good at managing resources. Then you'll see this last box right here. And this is the image box. So I have the option to choose a specific Docker image here to deploy this session. So that can help control additional environment variables, things like what R versions are available, Python versions, what packages are installed by default. That can all be controlled using various Docker images. But I'll just stick with the default for right now. That all looks good. Let's go ahead and start the session.
So once I do that, you can see this session is starting. And one of the other advantages of Posit Workbench is if I want to start another session at the exact same time, I can do that. I can have more than one VS Code session going. I can have an RStudio session going. I can have a Jupyter. We'll open a Jupyter Notebook session going. And this is a great way if you're working on multiple projects, using different languages and different IDEs, you can have them all going simultaneously and easily switch between the four that I've created here.
So we're going to open up this first VS Code session you can see down here at the bottom. And we'll just have that ready once we get the Dash application going here.
Setting up the Dash application in VS Code
So here's VS Code, again, running on Posit Workbench. On the left-hand side, we see my file explorer, and I've already created a blank directory. There's nothing in it. I just called it Quickstart. And this is where we're going to house that Dash application we're eventually going to deploy.
So where are we going to find this Dash application? So coming back to our demo environment of Posit Team here, we've already started within Workbench. Let's go ahead and open up Posit Connect.
So when you click on Posit Connect or open up Posit Connect, you'll see this screen that pops open. These are our jumpstart examples right here. And you can see there's a variety of content for R-related content. So R Markdown, parameterize R Markdown, Plumber API, Shiny apps, pins. You can choose any of these to get started. You can also select various Python content that's supported by Connect. So Jupyter Notebooks, Dash, Bokeh, Streamlit, various other ones, Python Shiny application, which is one of our newest supported applications.
But for today's demo, we're actually going to stick with the stock pricing dashboard. And probably for future sessions, we'll probably focus on some of these other jumpstart examples. But we thought this would be a great place to start. So we're going to go ahead and we're going to download this Dash application. We'll put it on Posit Workbench, and then we'll deploy it to Posit Connect. So let's go ahead and get started.
So with the jumpstart examples, the first thing that we need to do is we need to download the files for this Dash application. So we have this download button. I'm going to download it to my computer. And then I'll click on next right here. And it says to unzip this file that we just downloaded locally. And then we can basically move on to the next step.
But I'm going to go ahead and open this up within Posit Workbench. So let me switch back over to VS Code, again, running within Posit Workbench. I'm going to grab that file that I just downloaded, and I'm going to drag it over here to my file directory. And now we have that zip file that I just downloaded from that jumpstart example within VS Code on Posit Workbench.
Now, the first thing we need to do is unzip this file and gather all of its contents. And we're going to do this using the terminal. So I'm going to click on this button in the top left corner. I like to call it a burger button. Click on that, and you'll see Terminal, New Terminal. All right, and if I do print working directory, just to see what directory I'm in, you can see I'm in that quickstart directory. And if I do ls to list all the files in that directory, it just has that single zip file.
So let's go ahead and unzip it. So I'm going to run unzip, and then we'll just give it the name of that zipped file and hit enter. And that's actually going to, when it unzips, the contents is within a separate directory called stock-dashboard-python. So I'm going to go ahead and navigate into that directory, so cd, to change the directory, stock-dashboard-python. Now I'm going to clear my screen. I can do that on my keyboard using Ctrl-L. If I do print working directory now, you can see I'm within that directory, that file that we just unzipped, and I can click on it right here, and we can see all the contents. And right here, app.py, this is the Dash application that we are going to run and deploy to Posit Connect.
Installing packages with pip and Posit Package Manager
All right, so let's come back to Posit Connect and keep working our way through those steps to the jumpstart example. The first thing we need to do is install the required packages using something known as pip, which is a typical installer for installing Python packages. And we're going to use pip to install all the packages that live inside this file, requirements.txt.
So let me come back to VS Code and just show you what that file looks like. So over here on the left-hand side, you can see requirements.txt. These are all the libraries, the Python libraries that I need to basically run this Dash application and the necessary versions. All right, so this is kind of good for my environment, making sure that all these are present to appropriately deploy this Dash application.
So let's go ahead and make sure all these packages are there. I'm going to copy this command from the jumpstart example, come back to VS Code, hit paste, and enter. It's going to print some logs to my screen. You can see a lot of these say requirement already satisfied. So that's good. It looks like that these packages, for the most part, were already present in my environment.
But I want to take a quick pause here, and I think it's worth noting or trying to determine where am I actually looking when I go to install these packages. And that's where Package Manager, Posit Package Manager, is going to come into play. So if you look on this line right here, looking in indexes, it provides a URL. This is actually a URL to our demo Posit Package Manager instance. So let me come back to our demo environment, and let's explore Package Manager a little bit.
Now, for the most part, Package Manager is a tool used by your system administrators or IT team to help set up the package environment for Workbench, for Connect, for your developers. But there is a user interface that developers can log into and see what packages are available to them. So let's get started.
So here's the user interface. And I first want to have everyone take a look on the left-hand side. Over here, you can see various R repositories that are set up in this instance of Package Manager. So things like a CRAN mirror, for example, or any internally developed R packages. We also consider bioconductor packages. So if anyone's doing any genomic analysis, high-throughput sequencing data analysis, you might be familiar with bioconductor.
But for today's demo, we're going to focus in on Python repos. And there's currently two in this example Package Manager instance. We see pypi and localpython. So just to describe what localpython is, if you're creating any of your own Python packages and you want to share them with your team, you can upload them to your instance of Package Manager and make it super easy for anyone else on your team and your company to leverage that package.
And then you see pypi. And pypi is one of the largest repositories for Python packages. And you can explore all the various packages currently within pypi. So, for example, I can search numpy, which is a pretty popular data science package within Python. We can get some information about it, like how to install it, what it requires. So Python version has to be greater than 3.8. What's the most recent version? And some other information as well.
But how do I pull in numpy from this instance of Package Manager, from this pypi repository? So by selecting pypi, and you can see the name right here in the top left corner, we're going to go to the Setup tab and talk a little bit more about how to leverage Package Manager.
And you're going to see this URL. Use this URL to receive the latest source packages. So when you go to install a package, if you install from this URL, it's going to pull in from this repository on your instance of Package Manager. And, for example, this is our, again, our demo environment of Posit Package Manager.
And then to just know a few other cool things you can do with Package Manager, you can see in the path here, Latest. By using this URL, I'm always going to pull in the latest and greatest version of numpy, for example. But maybe that's not exactly what you want to do. Maybe you have a project that you worked on, you know, a year ago, and you want to go back into that project and you want to install packages as they were a year ago. And that's where this calendar comes into play. So I can go all the way back, let's say December 1st, 2021. If I click on that date, you can see the URL has changed. And now the date is reflected in the path. And if I install a package using this URL, it'll install it as if it's December 1st, 2021.
And that's where this calendar comes into play. So I can go all the way back, let's say December 1st, 2021. If I click on that date, you can see the URL has changed. And now the date is reflected in the path. And if I install a package using this URL, it'll install it as if it's December 1st, 2021.
And then if you ever want to learn how to actually leverage this URL in your instance of VS Code or wherever you're doing your Python development, we have some instructions right down here to install one-off packages from Package Manager or have it set more persistently. So any package you ever install will always use this URL. And that's what I've pretty much done in my environment. So that's Package Manager.
Deploying to Posit Connect
So let's come back here to Connect. So we've already done this step. We've installed all those packages. We made sure they're available using this requirements.txt file. Let's move on to the next step. And now we're going to install this rsconnect-python package. This is the actual package that's used to deploy Python content to Posit Connect. And we're going to install it using pip again. So pip install rsconnect-python-package.
So I'm going to go ahead and copy this command. Come back to VS Code. I'm going to clear my screen just to make it a little bit cleaner. So Control-L. I'm going to hit Paste and hit Enter. Again, you can see it's pulling from that same instance of Posit Package Manager in our demo environment. And for the most part, it looks like everything that this package depends on, the package itself was already satisfied. It's already present in my environment. So that's great.
So I'll clear my screen here, come back to Connect, and let's move on to the next step, which is really the last step. We are going to go ahead and deploy this application using the rsconnect-python package. Now there's a note right here. You will need to create and use an API key in order to deploy your Python application. And this is important because when you deploy to Posit Connect using this package, the rsconnect-python package, Connect needs to know who is actually trying to deploy and make sure you have the necessary credentials and the privileges to actually deploy to that instance of Posit Connect.
So right here is the command that we're going to use to deploy our Dash application. I'm going to go ahead and copy it, bring it on over to VS Code, again, running in Posit Workbench, and just paste it. I'm not going to run it just yet, but let's run through this command because it reads pretty easily from left to right. So we're using that rsconnect-python package, so this function right here, to deploy a Dash application to this server, this instance of Posit Connect.
And then you can see this next argument, API key, and then there's some placeholder text right here. So this is where you're going to supply that API key to let Connect know that, hey, it's me deploying this Dash application to that instance of Posit Connect. So where do we find this API key? Let's go back to Posit Connect, which I've already logged into. I'm going to close out of this jumpstart example because we're pretty much done with it for the most part. And I'm going to go over here to my name in the top right corner.
And you'll see API keys. So you can click on API keys, and you can see I've already created about four or so API keys. If you need to create a brand new one, you just select this box right here, copy that API key, come on back over to VS Code, running in Posit Workbench, and paste it right here. Now API keys, you should always treat just like you would any password, so you want to make sure they're nice and safe. I've actually already added this to my environment, so I'm just going to delete this argument. And the rsconnect Python package will automatically detect it when it runs this command. And then the last little bit, this dot and then the forward slash right here, rsconnect will automatically detect this app.py application in the directory and deploy it. So that's it. This is the command. We'll hit enter, and we'll let it run.
So it looks like I may have, so let me just check the print working directory. Let's try the command one more time. Oh, I got to delete the API key. There we go. And hit enter. There we go. So it's going through a few checks right here, like validating the server, making sure that bundle looks good, everything seems to be in order, okay. If anything's not in order, it should print some nice useful messages for you to help troubleshoot.
But what's actually happening right now? Like when you deploy this application, what's going on? So for the most part, it's capturing my environment. It's looking to see what Python version am I using? What libraries am I using? What versions of the libraries? So you can see like right here, for example, all these various Python libraries and their associated versions. It's making sure Connect has those packages and the appropriate versions on the server and can replicate my environment as exactly as it's laid out here. Once it's established it can, it then deploys that dash application.
So you can see deployment completed successfully. And then it gives me two links right here. We'll select that top one, click on this, and it's going to bring us right to Posit Connect. And while this is running, kind of setting up right here, you'll notice as a publisher, I have this little gear icon in the top right corner. And these are some additional controls I have as a publisher of this dash application. But here it is. This is the dash application now hosted on Posit Connect.
And we can try it out. We can hover our mouse over here. Some of the plots, you can see a little pop-up. I can choose another stock. So let's try Google. You can see that one pops up as well. Scrolling down, there's a few other interactive plots and visualizations. So here I could zoom in on a specific date. This plot's pretty cool. I can select certain points and you can see those points being selected in the companion plot right to the left here, kind of a cool crosstalk feature. And then we also have a table over here as well.
Sharing and configuring the deployed application
So this is the dash application. Now again, as a publisher, I have some additional controls. And the most important thing is I'm a publisher. How do I then share it with the people that need to see it? And that's going to be right here under access controls. Now we have it set by default to specific users or groups. And if you look right underneath, you can define those users and groups right here. I'm currently the only one that can view this content. But let's say, for example, I want to give Rachel access. Now we would be the only two that can view this content. So really tight control over who you want to share this application with, which is great, especially if you have any sensitive data.
But we can be a little less strict. So if you look at the middle option right here, all users log in required. If I select this, basically as long as you can log into the Connect instance, you can view this content. So here at Posit, it's our demo environment. That's pretty much all Posit employees. They can log into the Connect instance. They can view it. But we can be even less strict and select anyone no login required. And if I do that, basically this application, this dashboard we created, when hosted on Connect, is opened up to the world. So if anyone has this URL on the top of the screen you see right here, they can view this content.
Now, if you look at this URL, you can see it's not the prettiest URL. There's some random letters and numbers up here. We can change that. We can give this a more customized URL. So I can come down here to content URL and I can just say dash app and hit save. And now we have this customized URL at the bottom of my screen, which we can then share with the world and anyone can view this content.
So let's talk about a few of the other options you have. Again, as a publisher, you have tight control over this dash application. And the next one we'll go to is runtime. And this is where you can control the compute resources or Python processes supporting this application. If it's a real lightweight application, doesn't require a lot of compute power, you can keep it very low. But if you have an application that's maybe crunching some large data or training a model, for example, you might want to allocate some additional Python processes.
So there are Python processes supporting this application while it's running. We can define a minimum number of processes, a maximum, which you can see here is set to three. And then you have max connections per process. Now, if I share that link with somebody and they click on that link, they become a connection to this application. So by default here, I can have up to 50 connections and up to three processes. So I could support up to 150 concurrent users. Now, that may be perfect for what you need. Maybe you want to tweak it. And you can do that, again, as a publisher. You don't have to worry about your administrators and talking to them and configuring it. You can do it as a publisher. So for example, I can come in here and I can give it 10 more additional processes. And then in this configuration, I can support up to 500 concurrent users. So 50 connections per process and up to 10 processes. So again, that's how you can control it as a publisher.
A few additional options here along the top, the scheduling tab. This actually does not apply to interactive content. So things like Dash, Streamlit, Bokeh, or within our ecosystem like Shiny applications. So scheduling jobs doesn't necessarily apply to interactive content. But if you are deploying things like R Markdown or Jupyter Notebooks or even Quarto documents, you can actually place code in those documents and schedule them to run. So this is great if you have any recurring jobs, maybe every morning when you come in, you can actually place them into any of those documents I just mentioned, host it on Connect, and you can schedule it to run however frequently you want.
And then just to round out the last two here, tags are just for organizing your content. So if you have maybe a study or a project you're working on and there's a variety of pieces of content associated with that project, you can create a tag to help easily organize and pull up that various content types. And the last one here is VARs. If your content requires some type of privileged access to like a database or an API, for example, you can come into the VARs tab right here and supply that password or API key so the application has those credentials to access that database or API, for example.
Recap
All right, so that's pretty much it. And just to recap again, what we did today, we started in Posit Workbench, we created that VS Code session, we downloaded the Dash application from the Jumpstart example hosted in Connect, so that stock example, and then we went through the process of deploying that application to Posit Connect and then sharing it with the world. And as we did that, we were installing packages from our instance of Posit Package Manager. So hopefully this Posit team overview was helpful and we're just going to go ahead and open it up to any questions from the audience, general discussion, and we'd be happy to answer any questions anybody has.
