PythonAnywhere simple and unique online working environment
My first series in this blog was about Python Cloud Computing: developing Flask application and using Openshift to properly manage and deploy it. This is a series that bring another possibility: why don't I complete this blog with practical hands-on using another Python Cloud Computing solution? The internet is filled with this Platform as a Service solution. Some specifically state that they are Python only solution, other are much more general and open in nature. Having practical hands-on on the usage of some of them will add another advantage to this blog. Although, I am not sure that I can dive to all of the existing Cloud Computing/PaaS solution. For the most part, I will be unable to explore at the fullest on cloud solution that require actual billing to use important features such as database access, e.g Google Cloud or Amazon Web Services. In this regard, I really love Openshift way of attracting developers to use their platform: a 1GB storage including user uploaded files and database. I don't think there are other PaaS solution out there that can surpass this free offering.Well then, lets start at our first stop : PythonAnywhere.
One striking different between PythonAnywhere and the usual Python Cloud Computing solution that we know of, is that you can totally work on it online using internet browser in developing your Python application. With this, you can bypass the usual delicacies on preparing a local workstation that meet cloud hosting service environment requirement and directly work inside your browser that connected to many consoles provided by Pythonanywhere, such as : Bash, Python/iPython 2.6/2.7/3.3 and MySQL. Want to edit your Python files using ViM through Bash console runs from within browser? No problem. Or simply edit your applications using Pythonanywhere provided editor runs in a browser? They got that for you!
I can predict that some of you may look at this PythonAnywhere online feature with skepticism : "It's always more enjoyable working in a full desktop environment! Why do I want to do all my work inside a browser?" Well, for example, with a great internet connection and a 10" iPad/Galaxy Tab, things can become very interesting for a traveling freelancer. And if you do need to work locally, it's also supported. It has interesting method of synching your application using DropBox (but I can't test it currently. PythonAnywere gives an error message about its current unavailability of using DropBox). Or, you can always git push your local application code into a remote repository, and then git pull it from within PythonAnywere bash console. As this method can be use in a free/basic package, I may not exaggerating too much when I said that working with Pythonanywere is a pleasure.
Running Openshift's Flask Bio Application in Pythonanywhere
To demonstrate its feature, lets run our previously crafted Openshift's Flask Bio Application into Pythonanywhere basic/free package. The availability of a working free plan, --or in other words, not a useless limited features plan--, is a good sign of a service that welcomed the Open Source community (e.g try to compare the free plan in Openshift and Heroku, and you get the idea). We will have a look whether it is reasonably easy to do this migration. But for a start, you can go ahead and signup for a PythonAnywhere free plan. The rest of this article will discuss how to use PythonAnywhere using this free plan.
Prepare a New Web Application
With a free plan, you will get one domain using this name pattern :
yourusername.pythonanywhere.com. Currently only web developer plan ($12/month) and above will have the ability to use custom domain. Once login, go to your dashboard and click the Web tab. Click Add a new web app, which will open a wizard dialog that will guide you on the creation of your new Python web application.
A new web application using your username as the subdomain
I find it to be slick and simple, but yet an effective interface. It'll prompt you with the framework selection dialog, that beside Django and Flask, also offers several popular Python web frameworks. Pick Flask as the web framework.
I think Pylon/Pyramid should be on those list
And Python 2.7/Python 3.3 as your Python environment. I am still choosing Python 2.7 by the way..
Your choice of Python version
And finally, the only configuration page for Flask..
This is perfect : flask application in a single *.py file
If you open the default Flask application created, you will see this source code in
1 2 3 4 5 6 7
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello from Flask!'
Just returning a nice welcome message.
And if you inspects its WSGI startup file in (my configuration)
/var/www/swdev_pythonanywhere_com_wsgi.py, you will see this source code:
1 2 3 4 5 6 7 8 9
import sys # add your project directory to the sys.path project_home = u'/home/swdev/mysite' if project_home not in sys.path: sys.path = [project_home] + sys.path # import flask app but need to call it "application" for WSGI to work from flask_app import app as application
This is important. As this will be the place where we will configured our startup application
Cloned Existing Repository
We already have our Flask Biography application hosted in Openshift at this address, and its code also pushed in this public github repository: github.com/pythonthusiast/bio. So, lets just clone this github repository using Pythonanywhere bash console. Click Consoles tab and choose Bash console from the available consoles:
All the available consoles. In free plan, you will have access to only two consoles (I believe that's enough in most cases)
As we are going to use Pythonanywhere online editing features to edit this application (and hereby git add/commit/push it to origin repository), we will have to register this machine as the legitimate author for the aforementioned Github repository. Inside the console issue the command :
ssh-keygen, afterward, add the content of the file
~/.ssh/id_rsa.pub into your own Github Account's SSH keys. You will have to forked first this repository into your own Github repository.
Cloned existing repository after registering this machine public SSH key
The above cloned repository is having an Openshift custom directory structure, saved in
~/bio directory. Whereas due to Pythonanywhere default WSGI startup file, our Pythonanywere site is stored in
~/mysite. Lets keep our Openshift's
~/bio directory structure intact, and lets create symbolic links for required resources from within
~/bio directory into
~/mysite. After several trial and error, this is all symbolic links that needed:
For the unaware, from within
~/mysite directory use the command
ln -s <target file> to create the required symbolic links
It feels like the infamous LAMP stack, where you already have a MySQL database ready to use. Judging by their FAQ, it seems like currently MySQL and SQLite are the only database supported in PythonAnywhere. The default database having the name pattern of
<username>$default. It seems like there is no limit of how many database you can create in a free plan. The only restriction is your 500mb space quota. An important task is to your database protected with password using the Database tab.
In our Flask Bio application, we only use single
wsgi/main.py file, even for its database configuration. This is okay in the previous Openshift application, as the database URL come from Openshift's environment variable. But how do we do this in PythonAnywhere? Although I have added this new environment variable in
The above environment variable won't get detected inside our Python Flask application. The only known work around is to do this from within our WSGI startup file. Here is the modified WSGI startup file:
1 2 3 4 5 6 7 8 9
import sys, os os.environ['LOCAL_DB_URL']='mysql://<user>:<password>@mysql.server/<databasename>' # add your project directory to the sys.path project_home = u'/home/swdev/mysite' if project_home not in sys.path: sys.path = [project_home] + sys.path # import flask app but need to call it "application" for WSGI to work from flask_app import application
Observe the changes there, where we simply import our Flask application using the name
application, because we are using the name
app. Our last modification would be to modify
wsgi/main.py, this way:
application.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL') if os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL') else os.environ.get('LOCAL_DB_URL')
As everything already configured and ready, now we have to construct our database by simply running our
mysite/flask_app.py file, by issuing this command:
~/mysite$ python flask_app.py. Don't forget that, it will only run successfully if you have already add LOCAL_DB_URL in
~/.profile., and run
source ~/.profile to let the environment variable active. The
__main__ code of this
flask_app.py file will simply drop and recreate the database, making the database readily available for use.
Finally, don't forget to reload your web application and try accessing your application using the URL pattern
http://<username>.pythonanywhere.com. In may case, you can open it here.
You have to reload the database for each source code changes
Notice anything unique in our hands-on using Pythonanywhere? No local working environment setup. Pretty interesting, right? Although we are using the already working Flask application, this doesn't stop us to completely work this application in online mode using only internet browser. Actually, this is not a unique thing anymore. Using cloud9.io IDE, you can work fully online using internet browser and have your application hosted in Heroku or Openshift. But as it is a third party IDE, you may or may not like it. The interesting point is, PythonAnywhere was designed to be use fully online using internet browser.
If your solution only use Python, PythonAnywhere brings great advantage to you : as it is a PaaS solution that locked down its technology using only Python. No setup required in your part. Just create an account, login and start working. PythonAnywhere really bring the term Anywhere in Python.
Stay tuned for my next Python Cloud Computing article!