App engine: Flask HTML Form

 Main.py


======================================================================

from flask import Flask, request, render_template

# Imports the Google Cloud client library

 

# If `entrypoint` is not defined in app.yaml, App Engine will look for an app

# called `app` in `main.py`.


app = Flask(__name__)

@app.route('/', methods=['POST','GET'])         # This defines when the function below will be called

 

def run_quickstart():

    if request.method == 'POST':

        data = request.form.to_dict(flat=True)  

        Comments = data['mycomments'] # Sends data to the form

        return render_template('comments.html', FormComments=Comments)   # Renders the page with the response

    else:

        # This code will run if a GET is received

        return render_template('index.html')

 

  

 

if __name__ == "__main__":

    # This is used when running locally only. When deploying to Google App

    # Engine, a webserver process such as Gunicorn will serve the app. This

    # can be configured by adding an `entrypoint` to app.yaml.

    app.run(host="127.0.0.1", port=8080, debug=True)


===============================================================

requirements.txt

===============================================================

Flask==1.1.1
==========================================================================
app.yaml
==========================================================================
runtimepython37
==========================================================================
templates/index.html
==========================================================================
<!DOCTYPE html>
<html lang="en">
 

 
 
 
<body>
 

 
 
 
<h2>Input</h2>
<form method="post" action="/">
Comments: <textarea name="mycomments" rows="10" cols="30"> </textarea><br> 
 <button type="submit">Submit</button>
 </form>
 
</body>
</html>
==========================================================================
templates/comments.html
==========================================================================
<!doctype html>
<h2>Comments</h2>
            <p>
                {% if FormComments %}
                    {{ FormComments }}
                {% else %}
                    Enter some text above.
                {% endif %}
            </p>
            </html>
=========================================================================

No comments:

Post a Comment

Office hours tomorrow(Tuesday) 5:00pm-6:00pm, 4/26/2021, 5:13 PM, English, 4/26/2021, 5:13 PM

Your assigned language is: English Classroom blog: googleclouduconn.blogspot.com 4/26/2021, 5:13 PM Office hours tomorrow(Tuesday) 5...