Search code examples
python-3.xazure-webappsazure-static-web-app

Azure Static App Vs Azure Web App (Python)


I've got a Python project I've been working on that builds a static HTML page. I'd like to be able to run the Python script that does some stuff then starts a Web page with a search box kind of like the Azure Web App tutorial takes the result of their input term and runs the bottom half of the Python script which renders the static HTML page where there is a button that will taken them back to the Search page where they could kick off the second half of the script without rerunning the first half.

https://learn.microsoft.com/en-us/azure/app-service/quickstart-python?tabs=flask%2Cwindows%2Cvscode-aztools%2Cvscode-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli

https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart

Is this possible via an Azure Static App or would I need to use an Azure Web App? Also how would I redirect the web app to half way through the Python script without re-running the first half?

Thanks


Solution

  • The functionality you're describing doesn't align perfectly with the idea of a "static" web application, since you're expecting some server-side processing (running parts of a Python script based on user input). Azure Static Web Apps primarily serve static content without any server-side processing. In your case, an Azure Web App (especially one designed for Python, like the Flask-based tutorial you linked) would be more appropriate. https://devblogs.microsoft.com/devops/comparing-azure-static-web-apps-vs-azure-webapps-vs-azure-blob-storage-static-sites/

    To avoid rerunning the first half of the Python script, you can:

    Use Flask's session or caching mechanisms to store the results of the first half of the script, so they don't need to be recalculated if the user returns to the search page or Split your script into functions, so you can call just the parts you need at any given time.

    below is how you may structure your flask app

    from flask import Flask, render_template, request, redirect, url_for, session
    
    app = Flask(__name__)
    app.secret_key = 'some_secret_key'
    
    @app.route('/', methods=['GET', 'POST'])
    def index():
        if request.method == 'POST':
            search_term = request.form.get('search_term')
            # Process the first half of your script here
            session['result_first_half'] = some_result
            return redirect(url_for('results', search_term=search_term))
        return render_template('search.html')
    
    @app.route('/results')
    def results():
        search_term = request.args.get('search_term')
        # Process the second half of your script here, using the results from the first half
        data_from_first_half = session.get('result_first_half')
        result = process_second_half(data_from_first_half, search_term)
        return render_template('results.html', result=result)
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    The root route displays a search box and processes the first half of your script when a search term is submitted. The results route processes the second half of your script and displays the results.

    The results from the first half of the script are stored in the Flask session to avoid recalculating them.