LESSON 8

 

Deploying to PythonAnywhere

Lesson 8

Screen 1 of 18

Course progress review

This is lesson 8 of 8

 

1) Components of a Web application

2) Web frameworks and Flask

3) Installing Python and packages

4) Creating your development environment

5) Building your first local Flask app

6) Running your first Flask app

7) Web application deployment

8) Deploying to PythonAnywhere

Lesson 8

Screen 2 of 18

Lesson 8 outcomes

This lesson prepares you to:

 

  • set-up a new user account on the PythonAnywhere website
  • build a new Flask app on the PythonAnywhere website
  • create a local ZIP file from your local Flask app
  • upload the local ZIP file to PythonAnywhere's website
  • extract the contents of the uploaded ZIP file to PythonAnywhere's file-system
  • configure, run and test your Flask app on PythonAnywhere

Lesson 8

Screen 3 of 18

What is PythonAnywhere?

PythonAnywhere is a PaaS provider that can host and run your Flask application in the cloud (Reitz and Schlusser 2016).  Watch the video from PythonAnywhere (2013) to learn about their service.

Lesson 8

Screen 4 of 18

Features of PythonAnywhere

A basic, ‘beginner’ user account with PythonAnywhere is free and includes a:

Lesson 8

Screen 5 of 18

  • prepared and configured remote production environment to host and run your Flask application
  • unique domain name and Uniform Resource Locator (URL), so end-users can access your Flask application from a client Web browser
  • Web server to serve clients’ requests for Web content, to your Flask application

New PythonAnywhere user

Complete the following steps to create a new user account with PythonAnywhere:

Lesson 8

Screen  6 of 18

  • Open a Web browser and go to: https://www.pythonanywhere.com
  • Choose ‘pricing and sign-up’ from the main menu (top-right)
  • Select the ‘Create a beginner account’ option
  • Enter your email address and preferred login credentials
  • Check the check-box to confirm you agree with PythonAnywhere’s terms and conditions
  • Select the ‘register’ button
  • Take the ‘Welcome to PythonAnywhere tour’ or decline by choosing ‘end tour
  • Follow the link provided in the account confirmation email, from PythonAnywhere, to verify your new user account
  • The Email address confirmed’ dialogue box will be shown

Your PythonAnywhere user account is set up.  Confirm you can access to your PythonAnywhere account by logging in with your chosen login credentials.  Stay logged in to your PythonAnywhere account for the remainder of this lesson.

Build a Flask app on PythonAnywhere

Complete the following steps to create a new Flask Web application on PythonAnywhere.

Lesson 8

Screen 7 of 18

  • From the main menu choose ‘Web’ (top right)
  • Select the ‘Add a new web app’ button (top-left)
  • Choose ‘next’ from the dialogue box to accept the domain name allocated by PythonAnywhere
  • Pick ‘Flask’ from the ‘Select a Python Web framework’ dialogue and then ‘next’  
  • Choose ‘Python 3.5 (Flask 0.11.1)’ from the ‘Select a Python version’ dialogue box
  • Note the path shown in the ‘The 'Quick-start new Flask project’ dialogue box (for recall later in this lesson).  

Your new, online, PythonAnywhere Flask Web application is accessible the from the dashboard (shown below).

Flask app ZIP file         1/2

Lesson 8

Screen 8 of 18

You can make deploying your application easier by consolidating your local Flask Web application files into a single, unified file, called a ‘ZIP file'.  To make a ZIP file...

You need to deploy the local Flask application you created in Lesson 5 to the PythonAnywhere service.

Deployment will replace your online Flask Web application with your local Flask Web application.

Flask app ZIP file         2/2

  • Use Windows file Explorer to navigate to your local Flask Web application project directory at

     

  • Press and hold (or right-click) on the ‘app’ directory at

     

  • Choose the ‘Send to’ option from the menu
  • Select the ‘Compress (zipped) folder’ option

Lesson 8

Screen 9 of 18

Complete the following steps to create a new ZIP file from your local Flask Web application files:

You now have a new ZIP file named ‘app.zip’, inside your local project directory at c:\~\myproject\app\app.zip 

c:\~\myproject\

c:\~\myproject\app

Upload ‘app.zip’ to PythonAnywhere

  • Log-in to your PythonAnywhere user account
  • Choose ‘Web’ from the main options menu (top right)
  • Goto the directory called ‘mysite’ (listed on the right)
  • Select the ‘Upload a file’ button
  • Navigate to ‘app.zip’ on your local computer, from within the file-upload dialogue box
  • Select ‘app.zip’ to upload the file

Lesson 8

Screen 10 of 18

Upload ‘app.zip’ to PythonAnywhere by completing the following steps

A copy of ‘app.zip’ is now inside your ‘mysite’ directory on PythonAnywhere’s file-system.

Extract ‘app.zip’ to PythonAnywhere

  • Use PythonAnywhere's ‘files’ menu to navigate to your uploaded ‘app.zip’ file
  • Select the ‘open bash console here’ hyper-link (top right)
  • A new bash console will open.  When the bash prompt is ready, type the following command into the bash console and then press return:

Lesson 8

Screen 11 of 18

Complete the following steps to extract the contents of your uploaded ZIP file (‘app.zip’) onto PythonAnywhere’s file-system:

Your Flask Web application files have been uploaded and extracted successfully to PythonAnywhere.

  • When the files have extracted, type the following command into bash console and press return to close the bash console:

 

  • Wait for the 'Console closed' message to appear in the bash console
unzip app.zip
exit

Configure PythonAnywhere Flask App                      1/2

  • Choose ‘Web’ from drop down (top right) or dashboard
  • Scroll down to the ‘code’ section
  • Select the hyper-linked path (to the left of ‘Source code’)
  • Add ‘/app/’ to end of the source code path
  • Choose the          symbol to save your changes

Lesson 8

Screen 12 of 18

Direct PythonAnywhere to your extracted files by completing the following steps:

Configure PythonAnywhere Flask App                      2/2

  • Select the hyper-link beside ‘WSGI configuration file’ to open a text editor window
  • Point to the location of your main ‘app.py’ file by adding ‘/app/’ to end of the ‘project_home’ path. To do this, change line 11

Lesson 8

Screen 13 of 18

project_home = u'/home/firstflaskapp/mysite/

to:

project_home = u'/home/firstflaskapp/mysite/app/

With the editor window open...

from:

  • Call your Flask Web application from your ‘app.py’ file To do this, change line 16
from flask_app import app as application

to:

from app import app as application

from:

  • When you have edited lines 11 and 16 as shown,  select ‘save’ (top right).
  • Exit the text editor by choosing ‘Web’ from the drop-down menu (top right)

Your online Flask Web application is configured and ready to run.

Run your Flask App on PythonAnywhere

Lesson 8

Screen 14 of 18

To run your Flask Web application on PythonAnywhere:

  • login to your PythonAnywhere user account
  • choose the ‘Web’ options menu (from top right menu or centre panel)
  • select the button labelled ‘Reload [your app name].pythonanywhere.com

Reload your online Flask Web application whenever you make changes to your online application files.  If you modify your local Flask Web application files,  upload the changed files to PythonAnywhere and reload your online Web application.

Verify your Flask Web application's deployment

Lesson 8

Screen 15 of 18

The URL for your Flask Web application is listed at the top of the ‘Web’ options menu  (after ‘Configuration for [your URL]’), as shown.

Verify your Web application has deployed successfully to PythonAnywhere by copying the URL and pasting it into the address bar of your Web browser.  If deployment was successful, you will see the ‘Hello, World!’ message, sent from your online Flask Web application, inside your browser’s window.

You can logout of PythonAnywhere or modify your Web application files online.

Lesson 8 review

You completed Lesson 8 on Deploying to PythonAnywhere successfully.  You can now:

 

  • deploy Flask Web applications from your local computer to the PythonAnywhere PaaS provider

Lesson 8

Screen 16 of 18

Course completed

After you have reviewed the useful resources on the next screen, you have finished the course.  

 

Congratulations!  We hope you enjoyed learning to develop your First Flask Web application on PythonAnywhere.

Lesson 8

Screen 17 of 18

Useful resources

Lesson 8

Screen 18 of 18

If you have problems using PythonAnywhere, check the PythonAnywhere (2018) help pages for support.

 

PythonAnywhere's developers also maintain a blog.  Their post ‘A beginner’s guide to building a simple database-backed Flask website on PythonAnywhere’ (Thomas 2015) is particularly useful to beginners.  The blog is a great resource for learning more about PythonAnywhere.

 

Microsoft’s (2016) support website provides a useful guide to creating ZIP files in Windows.
 

Made with Slides.com