|
| 1 | +--- |
| 2 | +title: "Publish Your Streamlit App" |
| 3 | +teaching: 20 |
| 4 | +exercises: 0 |
| 5 | +questions: |
| 6 | +- "How do I deploy my app so other people can see it?" |
| 7 | +- "How do I create a `requirements.txt` file?" |
| 8 | +objectives: |
| 9 | +- "Learn how to deploy a Streamlit app" |
| 10 | +keypoints: |
| 11 | +- "All Streamlit apps must have a GitHub repo with the code, data, and environment files" |
| 12 | +- "You can deploy up to 3 apps for free with Streamlit Cloud" |
| 13 | +--- |
| 14 | + |
| 15 | +Now that we have our final app, it's time to publish (a.k.a deploy) it so that other people can see and interact with the visualizations. |
| 16 | + |
| 17 | +## Create a GitHub repo |
| 18 | + |
| 19 | +The first step is to create a new GitHub repository that will contain the code, data, and environment files. |
| 20 | + |
| 21 | +> ## Refresher on creating GitHub repos |
| 22 | +> If you have not created a GitHub repository before or need a refresher, please refer to [this episode on Remotes in GitHub](https://swcarpentry.github.io/git-novice/07-github/index.html) from the Software Carpentries lesson [Version Control with Git](https://swcarpentry.github.io/git-novice/) |
| 23 | +{: .callout} |
| 24 | + |
| 25 | +Give your repo a descriptive name, like `interact-with-gapminder-data-app`. Make sure that the repo is "Public" (not Private) and check the box to initialize the repo with a README.md. |
| 26 | + |
| 27 | +Once your repo is created, clone a copy of it to your local computer. You can use the application GitHub Desktop to more easily accomplish this. You can download GitHub Desktop [here](https://desktop.github.com). |
| 28 | + |
| 29 | +Now that you have a copy of your repo on your computer, you can copy over your code and data files. |
| 30 | + |
| 31 | +## Copy over the code and data files |
| 32 | + |
| 33 | +During this workshop, we have created some Jupyter Notebooks files and an `app.py` file. We don't need the Jupyter Notebooks to be a part of the app's repo, only the `app.py` file. |
| 34 | + |
| 35 | +Remember that at the start of the `app.py` file, we import our data with the line: |
| 36 | + |
| 37 | +~~~ |
| 38 | +df = pd.read_csv("Data/gapminder_tidy.csv") |
| 39 | +~~~ |
| 40 | +{: .language-python} |
| 41 | + |
| 42 | +So, we also need to make sure to include the `Data` folder and the `gapminder_tidy.csv` file inside of it. |
| 43 | + |
| 44 | +You can copy these files using a GUI (Finder on Macs or Explorer on PCs) or the command line, whatever you are comfortable with. |
| 45 | + |
| 46 | +Let's suppose that your repo is named `interact-with-gapminder-data-app`, and it has been cloned to the `GitHub` folder in your `Documents`. So, the location of your local repo is `~/Documents/GitHub/interact-with-gapminder-data-app`. So far, we have been working from the `Desktop`, in a folder called `data_viz_workshop`. Using the command line, we can copy the relevant files with: |
| 47 | + |
| 48 | +~~~ |
| 49 | +cp ~/Desktop/data_viz_workshop/app.py ~/Documents/GitHub/interact-with-gapminder-data-app/app.py |
| 50 | +mkdir ~/Documents/GitHub/interact-with-gapminder-data-app/Data |
| 51 | +cp ~/Desktop/data_viz_workshop/Data/gapminder_tidy.csv ~/Documents/GitHub/interact-with-gapminder-data-app/Data/gapminder_tidy.csv |
| 52 | +~~~ |
| 53 | +{: .language-bash} |
| 54 | + |
| 55 | +Your interact-with-gapminder-data-app folder should now look like: |
| 56 | + |
| 57 | +~~~ |
| 58 | +interact-with-gapminder-data-app |
| 59 | +│ README.md |
| 60 | +│ app.py |
| 61 | +│ |
| 62 | +└───Data |
| 63 | + │ gapminder_tidy.csv |
| 64 | +~~~ |
| 65 | +{: .output} |
| 66 | + |
| 67 | +We're almost done! But we need to do one more thing... specify an environment for the app to run in. |
| 68 | + |
| 69 | +## Add a requirements.txt file |
| 70 | + |
| 71 | +In order for our app to run on Streamlit's servers, we need to tell it what the environment should look like - or what packages need to be installed. While there are many standards for describing a python environment (such as the `environment.yml` file we used to create our conda environment at the beginning of this workshop), Streamlit tends to work best with a `requirements.txt` file. |
| 72 | + |
| 73 | +The `requirements.txt` file is just a list of package names and version numbers, with each line looking like `packagename==version.number.here`. And there is no need to list out all of the packages, just the top-level ones. The dependencies will get installed for each listed package as well. |
| 74 | + |
| 75 | +The `requirements.txt` file should be kept in the root of your repository. So, after creating a requirements file your repo should look like: |
| 76 | + |
| 77 | +~~~ |
| 78 | +interact-with-gapminder-data-app |
| 79 | +│ README.md |
| 80 | +│ requirements.txt |
| 81 | +│ app.py |
| 82 | +│ |
| 83 | +└───Data |
| 84 | + │ gapminder_tidy.csv |
| 85 | +~~~ |
| 86 | +{: .output} |
| 87 | + |
| 88 | +For our app, we only need to specify two packages: Streamlit and Plotly. Note that you may have different version numbers. Here is an example of our `requirements.txt` file: |
| 89 | + |
| 90 | +~~~ |
| 91 | +streamlit==1.1.0 |
| 92 | +plotly==5.1.0 |
| 93 | +~~~ |
| 94 | +{: .source} |
| 95 | + |
| 96 | +## Push the updated repo |
| 97 | + |
| 98 | +Now that we have our code, data, and environment files added to our local copy of the repository, it's time to push our repo to GitHub. You can use GitHub Desktop or use git from the command line to accomplish this. If you have unwanted extra files (like `.DS_Store`) added to the directory, make sure to add them to a `.gitignore` file! |
| 99 | + |
| 100 | +Once you have added, committed, and pushed your changes (with a descriptive commit message!) go to your GitHub repo online to double check that it is up to date. |
| 101 | + |
| 102 | +Now we are ready to deploy the app! |
| 103 | + |
| 104 | +## Sign up for Streamlit Cloud (Community Tier) |
| 105 | + |
| 106 | +Follow the steps detailed on Streamlit's Documentation to sign up for a free Streamlit Cloud account and connect it to your GitHub account. The documentation is located [here](https://docs.streamlit.io/streamlit-cloud/get-started). |
| 107 | + |
| 108 | +You can sign up for a Streamlit Cloud account at [share.streamlit.io/](https://share.streamlit.io/). |
| 109 | + |
| 110 | +On the free Community tier, you can deploy up to 3 apps at once. If you later decide to build more Streamlit apps, you can remove this one. |
| 111 | + |
| 112 | +## Deploy your app |
| 113 | + |
| 114 | +When you have signed in to your Streamlit account, click "New app". Copy and paste your GitHub repo URL, for example `https://github.com/username/interact-with-gapminder-data-app`, specify the correct branch name (which is most likely `main`), and specify the filename of your app (`app.py`). Then click "Deploy", and wait for the balloons! |
| 115 | + |
| 116 | +You can read the documentation about this process for deploying an app [here](https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app). |
| 117 | + |
| 118 | +{% include links.md %} |
| 119 | + |
0 commit comments