How to display python output on html page flask

Sure! Below is an example of how you can display Python output on an HTML page using Flask.

Assuming you have a Flask application with an endpoint to render the HTML page, you can use the Jinja templating engine to pass the Python output to your HTML template.

Here’s an example:

“`python
from flask import Flask, render_template

app = Flask(__name__)

@app.route(‘/’)
def index():
# Python code to generate output
python_output = “Hello, this is the Python output!”

# Render the HTML page and pass the Python output to it
return render_template(‘index.html’, output=python_output)

if __name__ == ‘__main__’:
app.run()
“`

In the above code, we define a Flask application with a single route (‘/’) that will render the HTML template. Inside the route function, you can write your Python code to generate the output you want to display on the HTML page. In this example, we assign a simple string “Hello, this is the Python output!” to the variable `python_output`.

Next, we use the `render_template` function to render the HTML template. We pass the `output` variable as a parameter to the template, which will be accessible as `{{ output }}` in the HTML code.

Now, create a file called `templates/index.html` in your project directory and add the following content to it:

“`html

Python Output:

{{ output }}

“`

In this HTML template, we have a `

` element that contains an `

` heading for the title and a `

` paragraph element where we will display the Python output. The `{{ output }}` is the placeholder that will be replaced with the actual value of `python_output` when rendering the template.

When you run the Flask application, visit `http://localhost:5000` (assuming Flask is running on the default port) in your browser, and you will see the HTML page with the Python output displayed.

Remember to install Flask and any other dependencies using `pip` before running the application:

“`
pip install flask
“`

I hope this helps you understand how to display Python output on an HTML page using Flask. Let me know if you have any further questions!

Leave a comment