Contents

PythonAnywhere Basic Usage

(Back to Blog)

PythonAnywhere is becomes popular recently among other modern and cool platforms with which you can develop your web applications, specifically in Python. And of course you can use it to host those of our web-related exercises which require publishing in the internet.

The only subtle thing is that it is not a simple hosting, but really "Platform" so it wants you to do many basic actions in their preferred way. As I am not acquainted with this resource, I tried it a bit and now could share basic actions you may be interested in.

Starting your Web-Application

After logging in you get to Dashboard. There would be main menu with Consoles, Files, Web etc. Since initially you have no applications running, you need to switch to Web tab.

Click Add a new web app here. Skip the popup about domain name and possible upgrade. Now you are asked about choosing your web-framework. If you (like me) are not acquainted with any, then click Manual configuration and select your favorite Python version. I've chosen 2.7. Click Next on few more popups. At last you are told that your web-app is running.

At this point you will see the url of your application. For example mine is http://rodiongork.pythonanywhere.com. You should check it is visible.

Adding static files

Serving static files is very simple. At the same Web tab where your application is shown (with two large green and red buttons above) - scroll down to subtitle Static files.

Here you can add forwarding for requests to such files. Click Enter URL and type in something like /files/ - that is a subfolder of your site. Now click Enter path and type some folder in your home folder, like /home/rodiongork/files.

Switch to Files tab and create this folder here. Add necessary files to it.

Now switch to Web tab again and click Reload (green button). From now the file should be visible, like this:

http://rodiongork.pythonanywhere.com/files/test.txt

Serving requests with Python

At the same Web tab you are told that your application is governed by file like this:

/var/www/rodiongork_pythonanywhere_com_wsgi.py

You can proceed from there to edit this file - or find it in Files tab. Anyway when you open it you will see:

You can simply add your own function below and attach to your own end-point. Try something like this:

@application.route('/random')
def throw_dice():
    import random
    return 'Random value is ' + str(random.randint(1, 6))

Click Save and then go back to Web tab of the Dashboard. Reload your application so that changes take effect. The result should be observable immediately. Here is mine:

http://rodiongork.pythonanywhere.com/random

Hope this short introduction may help you to dive into wonders of web-programming with Python and its nice frameworks!