I’ve had my Reclaim Hosting package for a bit over a year now, and now really done anything with it, so I had a quick dabble tonight looking for a way of installing and running a simple Python Flask app.
Searching around, it seems that CPanel offers a way in to creating a Python application:
Seems I then get to choose a python version that will be installed into a virtualenv for the application. I also need to specify the name of a folder in which the application code will live and select the domain and path I want the application to live at:
Setting up the app generates a folder into which to put the code, along with a public folder (into which resources should go) and a passenger_wsgi.py file that is used by a piece of installed sysadmin voodoo magic (Phusion Passenger) to actually handle the deployment of the app. (An empty folder is also created in the public_html folder corresponding to the app’s URL path.)
Based on the Minimal Cyborg How to Deploy a Flask Python App for Cheap tutorial, passenger_wsgi.py needs to link to my app code.
Passenger is a web application server that provides a scriptable API for managing the running of web apps (Passenger/py documentation).
For runnin Pyhon apps, we is used to launch the applicationif you change the wsgi file, I think yo
A flask app is normally called by running a command of the form python app.py on the commandline. In the case of a python application, the Passenger web application manager uses a passenger_wsgi.py file associated with the application to manage it. In the case of our simple Flask application, this corresponds to creating an object called application that represents it. If we create an application in a file myapp.py, and create a variable application that refers to it, we can run it via the passenger_wsgi.py file by simply importing it: from myapp import application.
WSGI works by defining a callable object called application inside the WSGI file. This callable expects a request object, which the WSGI server provides; and returns a response object, which the WSGI server serializes and sends to the client.
Flask’s application object, created by a MyApp = Flask(__name__) call, is a valid WSGI callable object. So our WSGI file is as simple as importing the Flask application object (MyApp) from app.py, and calling it application.
But first we need to create the application – for our demo, we can do this using a single file in the app directory. First create the file:
then open it in the online editor:
Borrowing the Minimal Cyborg “Hello World” code:
from flask import Flask app = Flask(__name__) application = app # our hosting requires application in passenger_wsgi @app.route("/") def hello(): return "This is Hello World!\n" if __name__ == "__main__": app.run()
I popped it into the myapp.py file and saved it.
(Alternatively, I could have written the code in an editor on my desktop and uploaded the files.)
We now need to edit the passenger_wsgi.py file so that it loads in the app code and gets from it an object that the Passenger runner can work with. The simplest approach seemed to be to load in the file (from myapp) and get the variable pointing to the flask application from it (import application). I think that Passenger requires the object be made available in a variable called application?
That is, comment out the original contents of the file (just in case we want to crib from them later!) and import the application from the app file: from myapp import application.
So what happens if I now try to run the app?
Okay – it seemed to do something but threw an error – the flask package couldn’t be imported. Minimal Cyborg provides a hint again, specifically “make sure the packages you need are installed”. Back in the app config area, we can identify packages we want to add, and then update the virtualenv used for the app to install them.
And if we now try to run the app again:
So now it seems I have a place I can pop some simple Python apps – like some simple Slack/slash command handlers, perhaps…
PS if you want to restart the application, I’m guessing all you have to do is click the Restart button in the appropriate Python app control panel.