Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Deploying Your Dash Application to Heroku

For more information on concepts covered in this lesson, you can check out:

00:00 Deploy your Dash application to Heroku. You’re done building your application, and you have a beautiful, fully interactive dashboard. Now you’ll learn how to deploy it.

00:13 Dash apps are Flask apps, so both share the same hosted deployment options. In this section, you’ll deploy your app on Heroku. Before you get started, make sure you’ve installed the Heroku Command-Line Interface and Git.

00:30 Heroku CLI is available for a wide range of operating systems, and the page link seen onscreen shows how to install them. Git is also cross-platform and is easy to install.

00:44 You can verify that both exist in your system by running these commands at a command prompt on Windows or a terminal on macOS or Linux.

00:57 The output you see may change slightly depending on your operating system and the version you have installed, but you shouldn’t get an error. With those two requirements present, let’s make a start on the deployment. Firstly, there’s a small change you need to make on app.py. After you initialize the app, add a new variable called server.

01:22 This addition is necessary to run your app using a WSGI server. It’s not advisable to use Flask’s built-in server in production since it won’t be able to handle much traffic.

01:35 Next, in the project’s root directory, create a file called runtime.txt, where you’ll specify a Python version for your Heroku app. When you deploy your app, Heroku will automatically detect that it’s a Python application and will use the correct buildpack. If you also provide a runtime.txt, then it’ll pin down the Python version that your app will use.

01:59 Next, create a requirements.txt file in the project’s root directory, where you’ll copy the libraries required to set up your Dash application on a web server.

02:13 You may have noticed there’s a package in requirements.txt you haven’t seen until now: Gunicorn. “Green unicorn” is a WSGI HTTP server that is frequently used for deploying Flask apps to production.

02:27 You’ll be using it to deploy your dashboard. Now, create a file named Procfile with the following content.

02:39 This file tells the Heroku app what commands should be executed to start your app. In this case, it starts a Gunicorn server for your dashboard. Next, you’ll need to initialize a Git repository. To do that, go to your project’s root directory and execute the following command.

02:59 This will start a Git repository in avocado_analytics/. It’ll start tracking all of the changes you make to the files in that directory. However, there are files you don’t want to track using Git. For example, you usually want to remove Python compiled files, the contents of your virtual environment folder, or metadata files such as .DS_Store.

03:23 To avoid tracking unnecessary files, create a file called .gitignore in the root directory. Then, enter the following content into it. This will make sure your repository doesn’t track unnecessary files.

03:41 Now, commit your project files using the two commands seen next.

03:57 Before the final step, make sure you have everything in place. Your project structure should look like this. Finally, you need to create an app in Heroku, push your code there using Git, and start the app using one of Heroku’s free server options.

04:17 To do this, you’ll need to create an account on heroku.com. Once you’ve done that, you’ll need to choose a unique name for your project. If you try to use a name that’s already in use, such as the one you’ll see me using onscreen, then you’ll get an error.

04:33 The project name will equate to the URL you’ll need to visit when you have uploaded the app, so keep that in mind when you create the project if you’re going to share it with other people.

04:44 You’ll now see the commands needed to do this onscreen. This command creates a new application on Heroku and an associated Git repository. This command pushes the changes to that repository.

05:16 And this third command starts your app in one of Heroku’s free server options.

05:27 And that’s it! You’ve built and deployed your dashboard. Now you just need to access it to share it with your friends. To access your app, use the APP-NAME and visit <APP-NAME>.herokuapp.com in your browser, as seen onscreen.

05:50 Now that you’ve completed the course and created a shareable, fully interactive dashboard, in the next section of the course, we’ll take a look back at everything you’ve learned.

Avatar image for Neph Pierre

Neph Pierre on Jan. 23, 2023

When I try do the git push heroku master. I get this error: ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to ‘git.heroku.com/rp-avacado-analysis.git

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 24, 2023

Neph Pierre you’ll need to use a unique project name—using rp-avocado-analysis won’t work.

You can learn more about the process in the Django tutorial on creating and deploying a Heroku app. The link takes you to the relevant section that focuses on Heroku. For this part, it doesn’t matter whether it’s a Flask or a Django app that you want to deploy.

Hope that helps!

Avatar image for Neph Pierre

Neph Pierre on Jan. 25, 2023

Hello Martin,

Thank you for responding I do have a unique project name. I ended up resolving that issue I posted yesterday. I have another error but I think its the last thing to get me to successfully deploy to heroku. I’m going to try to attend the Q&A call tomorrrow if I am not able to resolve before then.

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 25, 2023

@Neph Pierre that’s good news and a great idea to join Office Hours! The real-time setting makes it quicker to find solutions to some problems :)

How did you resolve the previous issue and do you know what caused it?

Avatar image for sant3e

sant3e on Aug. 4, 2023

I’m on windows 10. I’ve installed heroku via the .exe available on their website. Nothing works. After installation, i don’t get to chose the safe mode or anything like that.. i just get a message that this can’t run on this pc. Via terminal also nothing works. So i had to stop the project

Become a Member to join the conversation.