'How can I execute a python flask web app with xampp?

I'm developing a web app in python with flask and I don't know how to test my app in xampp apache server. When I go to the xampp dashboard and open the .py file, the browser only shows me the .py code and doesn't execute anything. The .py file hasn't get html code. I want to know how to execute the .py file to see the .html and its funcionality in the browser. This is the code of the .py file:

#C:\Python27\python.exe -u
from flask import Flask, render_template, session, request, escape, 
url_for, redirect
from hashlib import md5
import MySQLdb, sys, pdb
import cgi

app = Flask(__name__)

# if __name__ == '__main__':
db = MySQLdb.connect(host="localhost", user="root",
                     passwd="root", db="prueba")
cur = db.cursor()
app.secret_key = 'mysecretkey'


class ServerError(Exception):
   pass

# METODO DE LA PAGINA HOME DE ADMIN


@app.route('/')
def home():

    #Si esta la sesion iniciada, cargamos la pagina home con la sesion iniciada

    if 'username' in session:
        username_session = escape(session['username']).capitalize()

        return render_template('home.html', 
session_user_name=username_session, adminIs = True)

    #Si no esta la sesion iniciada, cargamos la pagina home sin la sesion iniciada

    return render_template('home.html')

app.secret_key='A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

if __name__ == '__main__':
    app.run(debug=True)

This code only shows one function to show the home page .html but I want the browser, when I go to one of the pages, show the .html (with embebed php code) the data I bring from mysql server.



Solution 1:[1]

You have to tell the apache server from xampp to execute python scripts. So edit the httpd.conf in your xampp/apache folder and add the following at the end of the file:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

Restart xampp and youre .py file should be executed.

Solution 2:[2]

i know it was posted long time ago, recently i had the similar requirement
i will demonstrate how to deploy python flask app on Apache2.4 with minimal dummy flask app ( same can be tested on your xampp server)

prerequisite

  • download Apache2.4 https://www.apachelounge.com/download/ unzip it and paste the Apache24 to C:/Apache24
  • add to path MOD_WSGI_APACHE_ROOTDIR="C:\Apache24"
  • install apache server as a windows server
        > C:\Apache24\bin\httpd.exe -k install
  • download the mod_wsgi from the https://www.lfd.uci.edu/~gohlke/pythonlibs/
        install the mod_wsgi wheel file
        > python -m pip install <*.whl file>
  • from command line run the following
        > mod_wsgi-express module-config
  • paste the above output to the httpd.conf file in the last line
  • paste the following after the previous step
  • <IfModule wsgi_module>
        <VirtualHost *:5000>
            ServerName 127.0.0.1
            WSGIScriptAlias /myapp C:/Apache24/htdocs/flaskapp/my_app.wsgi
            <Directory C:/Apache24/htdocs/flaskapp>
                Options +Indexes +FollowSymLinks +Includes +ExecCGI
                Allow from all
                AllowOverride None
                Require all granted
            </Directory>
            ErrorLog "D:/var/logs/error.log"
            CustomLog "D:/var/logs/access.log" common
        </VirtualHost>
    </IfModule>  
    

  • my application resides in C:\Apache24\htdocs\flaskapp which has my_app.py and my_app.wsgi
  • Restart the Apache24 service from windows service management
        windows + R --> services.msc --> search for Apache24 --> restart
  • go to browser
        http://localhost:5000/myapp

    NOTE: myapp is an alias name given in the VirtualHost tag

  • Solution 3:[3]

    app.config['SQLALCHEMY_DATABASE_URI']='mysql://medclub:sandip123@localhost:3306/dbsite'`

    This works for me.

    Sources

    This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

    Source: Stack Overflow

    Solution Source
    Solution 1 youwhatmate
    Solution 2 Abhishek D K
    Solution 3 Sandip Mahato