Flask: Difference between revisions

No edit summary
m Remove unneeded table captions
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Flask =
== Flask ==


= About Flask =
== About Flask ==


Flask is a Python web framework built with a small core and easy-to-extend philosophy. Read more at [Full Stack Python](https://www.fullstackpython.com/flask.html).
Flask is a Python web framework built with a small core and easy-to-extend philosophy. Read more at [https://www.fullstackpython.com/flask.html Full Stack Python].


= Johnny server =
== Morty server ==


If you need to run Flask on another version of Python, you'll need to get a [https://heliohost.org/vps/ VPS]. You can get a 10% discount when you pay for 6 months upfront.
{{Info|If you need to run Flask on another version of Python, you'll need to get a [https://heliohost.org/vps/ VPS]. You can get a 10% discount when you pay for 6 months upfront.}}


{| class="wikitable" style="margin:auto"
{| class=="wikitable" style=="margin:auto"
|+ Johnny server
|-
|-
! Server !! Flask Version !! Python Version !! Python Modules Installed !! Python Path !! Loader
! Server !! Flask Version !! Python Version !! Python Modules Installed !! Python Path !! Loader
|-
|-
| Johnny || 3.0.3 || 3.12 || [https://krydos2.heliohost.org/pyinfo/info3.12.py View] || /usr/bin/python3.12 || WSGI
| Morty || 5.0.7 || 3.12 || [https://krydos3.heliohost.org/pyinfo/info3.12.py View] || /usr/bin/python3.12 || WSGI
|}
|}


= Tommy server =
== Tommy server ==


If you need to run Flask on another version of Python, you'll need to get a [https://heliohost.org/vps/ VPS]. You can get a 10% discount when you pay for 6 months upfront.
{{Info|If you need to run Flask on another version of Python, you'll need to get a [https://heliohost.org/vps/ VPS]. You can get a 10% discount when you pay for 6 months upfront.}}


{| class="wikitable" style="margin:auto"
{| class=="wikitable" style=="margin:auto"
|+ Tommy server
|-
|-
! Server !! Flask Version !! Python Version !! Python Modules Installed !! Python Path !! Loader
! Server !! Flask Version !! Python Version !! Python Modules Installed !! Python Path !! Loader
Line 29: Line 27:
|}
|}


= How To Setup Flask on Plesk =
== Johnny server ==
 
{{Info|If you need to run Flask on another version of Python, you'll need to get a [https://heliohost.org/vps/ VPS]. You can get a 10% discount when you pay for 6 months upfront.}}
 
{| class=="wikitable" style=="margin:auto"
|-
! Server !! Flask Version !! Python Version !! Python Modules Installed !! Python Path !! Loader
|-
| Johnny || 3.0.3 || 3.12 || [https://krydos2.heliohost.org/pyinfo/info3.12.py View] || /usr/bin/python3.12 || WSGI
|}
 
== How To Setup Flask on Plesk ==


= Create a directory on your main domain called `flasktest`. =
== Create a directory on your main domain called 'flasktest'. ==


If you were transferred from the old cPanel, your main domain will be parked on the `public_html` directory.   
If you were transferred from the old cPanel, your main domain will be parked on the 'public_html' directory.   


If you created a new account on Plesk, your directory will be `httpdocs`.
If you created a new account on Plesk, your directory will be 'httpdocs'.


=Create an `.htaccess` file inside the `flasktest` directory with these contents: =
==Create an '.htaccess' file inside the 'flasktest' directory with these contents: ==


<pre>
<pre>
Line 49: Line 58:
</pre>
</pre>


= Create a file named `flask.wsgi` inside the `flasktest` directory with these contents: =
== Create a file named 'flask.wsgi' inside the 'flasktest' directory with these contents: ==


<pre>
<pre>
Line 61: Line 70:


# set this to something harder to guess
# set this to something harder to guess
application.secret_key = 'secret'
application.secret_key == 'secret'
</pre>
</pre>


= Create a file named `myapp.py` inside the `flasktest` directory with these contents: =
== Create a file named 'myapp.py' inside the 'flasktest' directory with these contents: ==


<pre>
<pre>
Line 70: Line 79:


from flask import Flask, __version__
from flask import Flask, __version__
app = Flask(__name__)
app == Flask(__name__)
application = app
application == app


@app.route("/")
@app.route("/")
Line 78: Line 87:


     Flask is working on HelioHost.<br><br>
     Flask is working on HelioHost.<br><br>
     <a href="/flasktest/python/version/">Python version</a><br>
     <a href=="/flasktest/python/version/">Python version</a><br>
     <a href="/flasktest/flask/version/">Flask version</a>
     <a href=="/flasktest/flask/version/">Flask version</a>


   """
   """
Line 85: Line 94:
@app.route("/python/version/")
@app.route("/python/version/")
def p_version():
def p_version():
   return "Python version %s<br><br><a href='/flasktest/'>back</a>" % sys.version
   return "Python version %s<br><br><a href=='/flasktest/'>back</a>" % sys.version


@app.route("/flask/version/")
@app.route("/flask/version/")
def f_version():
def f_version():
   return "Flask version %s<br><br><a href='/flasktest/'>back</a>" % __version__
   return "Flask version %s<br><br><a href=='/flasktest/'>back</a>" % __version__


if __name__ = "__main__":
if __name__ == "__main__":
   app.run()
   app.run()
</pre>
</pre>


= Make sure your directory structure and files look like this: =
== Make sure your directory structure and files look like this: ==


<pre>
<pre>
Line 106: Line 115:
</pre>
</pre>


= If you did everything right it should look like the below: =
== If you did everything right it should look like the below: ==


Screenshot taken from [https://krydos.heliohost.org/flasktest https://krydos.heliohost.org/flasktest/]
Screenshot taken from [https://krydos.heliohost.org/flasktest https://krydos.heliohost.org/flasktest/]
Line 112: Line 121:
[[File:flask_test.png]]
[[File:flask_test.png]]


= WSGI Uses Caching =
== WSGI Uses Caching ==


{{Caution|
{{Caution|
Line 120: Line 129:
}}
}}


= What Caching Does =
== What Caching Does ==


Multiple Apache processes are running on the server, and each time you refresh your site you are randomly assigned to one of these processes. If that particular process has already displayed your site, it shows the cached version of your code; otherwise, it shows the new code changes. This means that during the first 2 hours after a site change, you may intermittently see old or new content, depending on which process you get assigned to. This situation will resolve when Apache is restarted, which happens every 2 hours.
Multiple Apache processes are running on the server, and each time you refresh your site you are randomly assigned to one of these processes. If that particular process has already displayed your site, it shows the cached version of your code; otherwise, it shows the new code changes. This means that during the first 2 hours after a site change, you may intermittently see old or new content, depending on which process you get assigned to. This situation will resolve when Apache is restarted, which happens every 2 hours.


= Options to Work Around Caching =
== Options to Work Around Caching ==


= Request WSGI Control Access =
== Request WSGI Control Access ==


A new feature currently in beta is the ability for users to restart their Flask app themselves.  
A new feature currently in beta is the ability for users to restart their Flask app themselves.  


To request this, please create a new post in the [https://helionet.org/index/forum/45-customer-service/?do=add Customer Service forum] and provide your '''username''', '''server name''', and the '''domain name(s)''' you want to be given WSGI Control Access for. (If you have 2 Flask apps on 2 different domains, you need to request WSGI Control Access for each domain.)
To request this, please create a new post in the [https://helionet.org/index/forum/45-customer-service/?do==add Customer Service forum] and provide your '''username''', '''server name''', and the '''domain name(s)''' you want to be given WSGI Control Access for. (If you have 2 Flask apps on 2 different domains, you need to request WSGI Control Access for each domain.)


Once you have been given WSGI Control Access, you can edit your `flask.wsgi` to reload your Flask app so new code changes load immediately. The edits to the file can be as simple as adding or removing a space or a blank line. As long as the file's `last modified date` changes it will discard the cache and reload your Flask app.
Once you have been given WSGI Control Access, you can edit your 'flask.wsgi' to reload your Flask app so new code changes load immediately. The edits to the file can be as simple as adding or removing a space or a blank line. As long as the file's 'last modified date' changes it will discard the cache and reload your Flask app.


Please let us know if you experience unexpected results with this new feature.
Please let us know if you experience unexpected results with this new feature.


= Account Resets Remove WSGI Control Access =
== Account Resets Remove WSGI Control Access ==


{{Info|
{{Info|
Line 142: Line 151:
}}
}}


= Use Local Development Environment =
== Use Local Development Environment ==


Another option to see code changes reflected immediately is to develop your Flask app on your home computer and then host the production copy on the server.
Another option to see code changes reflected immediately is to develop your Flask app on your home computer and then host the production copy on the server.


= VPS =
== VPS ==


You may prefer to explore one of our paid [https://heliohost.org/vps/ VPS plans], depending on your requirements. You can get a 10% discount when you pay for 6 months upfront.
You may prefer to explore one of our paid [https://heliohost.org/vps/ VPS plans], depending on your requirements. You can get a 10% discount when you pay for 6 months upfront.


= Troubleshooting =
== Troubleshooting ==


If you receive an error of `You don't have permission to access /flasktest/flask.wsgi/ on this server`, you may need to add `AddHandler cgi-script .py` on a new line in your `.htaccess` file.
If you receive an error of 'You don't have permission to access /flasktest/flask.wsgi/ on this server', you may need to add 'AddHandler cgi-script .py' on a new line in your '.htaccess' file.


= Running Flask at the Webroot =
== Running Flask at the Webroot ==


The above example is for Flask in a subdirectory, so users can host different projects in different subdirectories rather than dedicating the whole domain to one Flask app.
The above example is for Flask in a subdirectory, so users can host different projects in different subdirectories rather than dedicating the whole domain to one Flask app.


If you prefer to run Flask at the webroot instead, this is possible with some slight changes to the `.htaccess`, `flask.wsgi`, and `myapp.py` files.
If you prefer to run Flask at the webroot instead, this is possible with some slight changes to the '.htaccess', 'flask.wsgi', and 'myapp.py' files.


= References =
== References ==


This tutorial is adapted from the [https://helionet.org/index/topic/53856-how-to-use-flask-on-plesk/ How to Use Flask on Plesk] post on the HelioNet forum, answered by Krydos.
This tutorial is adapted from the [https://helionet.org/index/topic/53856-how-to-use-flask-on-plesk/ How to Use Flask on Plesk] post on the HelioNet forum, answered by Krydos.

Latest revision as of 18:46, 17 January 2025

Flask

About Flask

Flask is a Python web framework built with a small core and easy-to-extend philosophy. Read more at Full Stack Python.

Morty server

If you need to run Flask on another version of Python, you'll need to get a VPS. You can get a 10% discount when you pay for 6 months upfront.

Server Flask Version Python Version Python Modules Installed Python Path Loader
Morty 5.0.7 3.12 View /usr/bin/python3.12 WSGI

Tommy server

If you need to run Flask on another version of Python, you'll need to get a VPS. You can get a 10% discount when you pay for 6 months upfront.

Server Flask Version Python Version Python Modules Installed Python Path Loader
Tommy 5.0.7 3.12 View /usr/bin/python3.12 WSGI

Johnny server

If you need to run Flask on another version of Python, you'll need to get a VPS. You can get a 10% discount when you pay for 6 months upfront.

Server Flask Version Python Version Python Modules Installed Python Path Loader
Johnny 3.0.3 3.12 View /usr/bin/python3.12 WSGI

How To Setup Flask on Plesk

Create a directory on your main domain called 'flasktest'.

If you were transferred from the old cPanel, your main domain will be parked on the 'public_html' directory.

If you created a new account on Plesk, your directory will be 'httpdocs'.

Create an '.htaccess' file inside the 'flasktest' directory with these contents:

Options +ExecCGI
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(flask\.wsgi/.*)$ - [L]
RewriteRule ^(.*)$ flasktest/flask.wsgi/$1 [QSA,PT,L]

Create a file named 'flask.wsgi' inside the 'flasktest' directory with these contents:

import os, sys

# edit your path below
sys.path.append("/home/domain.helioho.st/httpdocs/flasktest");

sys.path.insert(0, os.path.dirname(__file__))
from myapp import app as application

# set this to something harder to guess
application.secret_key == 'secret'

Create a file named 'myapp.py' inside the 'flasktest' directory with these contents:

import sys

from flask import Flask, __version__
app == Flask(__name__)
application == app

@app.route("/")
def hello():
  return """

    Flask is working on HelioHost.<br><br>
    <a href=="/flasktest/python/version/">Python version</a><br>
    <a href=="/flasktest/flask/version/">Flask version</a>

  """

@app.route("/python/version/")
def p_version():
  return "Python version %s<br><br><a href=='/flasktest/'>back</a>" % sys.version

@app.route("/flask/version/")
def f_version():
  return "Flask version %s<br><br><a href=='/flasktest/'>back</a>" % __version__

if __name__ == "__main__":
  app.run()

Make sure your directory structure and files look like this:

flasktest/
├── flask.wsgi
├── .htaccess
└── myapp.py

0 directories, 3 files

If you did everything right it should look like the below:

Screenshot taken from https://krydos.heliohost.org/flasktest/

WSGI Uses Caching

Flask changes can take up to 2 hours to appear consistently on your site because WSGI uses server side caching.

If you want site changes to take effect immediately, we offer a few options to work around caching.

What Caching Does

Multiple Apache processes are running on the server, and each time you refresh your site you are randomly assigned to one of these processes. If that particular process has already displayed your site, it shows the cached version of your code; otherwise, it shows the new code changes. This means that during the first 2 hours after a site change, you may intermittently see old or new content, depending on which process you get assigned to. This situation will resolve when Apache is restarted, which happens every 2 hours.

Options to Work Around Caching

Request WSGI Control Access

A new feature currently in beta is the ability for users to restart their Flask app themselves.

To request this, please create a new post in the Customer Service forum and provide your username, server name, and the domain name(s) you want to be given WSGI Control Access for. (If you have 2 Flask apps on 2 different domains, you need to request WSGI Control Access for each domain.)

Once you have been given WSGI Control Access, you can edit your 'flask.wsgi' to reload your Flask app so new code changes load immediately. The edits to the file can be as simple as adding or removing a space or a blank line. As long as the file's 'last modified date' changes it will discard the cache and reload your Flask app.

Please let us know if you experience unexpected results with this new feature.

Account Resets Remove WSGI Control Access

If you request an account reset you will need to re-request WSGI Control Access after the reset has been completed. By default, account resets will disable WSGI Control Access.

Use Local Development Environment

Another option to see code changes reflected immediately is to develop your Flask app on your home computer and then host the production copy on the server.

VPS

You may prefer to explore one of our paid VPS plans, depending on your requirements. You can get a 10% discount when you pay for 6 months upfront.

Troubleshooting

If you receive an error of 'You don't have permission to access /flasktest/flask.wsgi/ on this server', you may need to add 'AddHandler cgi-script .py' on a new line in your '.htaccess' file.

Running Flask at the Webroot

The above example is for Flask in a subdirectory, so users can host different projects in different subdirectories rather than dedicating the whole domain to one Flask app.

If you prefer to run Flask at the webroot instead, this is possible with some slight changes to the '.htaccess', 'flask.wsgi', and 'myapp.py' files.

References

This tutorial is adapted from the How to Use Flask on Plesk post on the HelioNet forum, answered by Krydos.


This page was last edited on 17 January 2025, at 18:46.