Flask App with Data Handling and Template Rendering

  • Share this:

Code introduction


This function creates a Flask application with two routes: one for handling POST requests and returning a JSON response, and another for rendering an HTML template.


Technology Stack : Flask, Jinja2

Code Type : Web Application

Code Difficulty : Intermediate


                
                    
from flask import Flask, request, jsonify, render_template

def create_flask_app():
    app = Flask(__name__)

    @app.route('/data', methods=['POST'])
    def data():
        data = request.json
        return jsonify(data)

    @app.route('/template', methods=['GET'])
    def template():
        return render_template('index.html', name='John Doe')

    return app

# JSON Response                
              
Tags: