Creating a Python "Hello World" application with Flask for ASO Shared Hosting
Posted by ASO Admin on 25 April 2014 02:52 PM
|
|
Preparing the Python EnvironmentThe first thing we need to do is set up a local Python environment. For this we'll be using VirtualEnv, which is commonly used to isolate Python environments. This tutorial will require SSH access to your account, which is covered <here>.
Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/USER/pyenv). At this time we're also selecting which python version we're going to be using for our environment, in the below example, we're choosing to use the server's default python.
After that last command, our prompt should now look a little different indicating we've entered our python environment. Now let's install Flask:
Creating your first Flask applicationJust in case you're revisiting this tutorial, please ensure we're still in or if not, change to your account's home directory:
Now that our environment has been set up to support Flask, we can create our first application using the Flask framework. For this tutorial we're going to create a standard Hello World application. First, let's create our application's directory:
And now change to our application's directory:
Now we need to create our basic application directory layout, which consists of three sub directories:
Now, let's create our main application file:
Inside this file, place these contents: from flask import Flask Save the file and close the file. Now we need to create an __init__.py file, this is actually going to be an empty file:
Preparing your new Flask application for PassengerNext, we need to create a file named "passenger_wsgi.py". This is the entry file to our application:
Inside this file, place the below contents substituting %USER% for your own user on the server: import sys, os Save and close the file. The final step is to create an .htaccess file in the document root we want to serve the application from. Let's assume it's the root of your website, which would be the public_html directory. Let's change to that directory:
Now, create or open the file named .htaccess:
Inside this file, place: PassengerEnabled on Save and close the file. Now try visiting your website, you should see "Hello World". | |
|