1

I am new to Flask, using which I am creating a webapp.

I have one main function (execute()) that I am running, and it has a number of print statements that get shown in the terminal when the function is run.

However, I would like to display those statements on the webpage as well through HTML. How can I do so?

I have tried searching for the answer, but nothing has worked so far.

app = Flask(__name__)

class MyForm(FlaskForm):
    image = MultipleFileField('image')

@app.route('/', methods=['GET', 'POST'])
def index():

    form = MyForm()

    if form.validate_on_submit():

        for file in form.image.data:
            image.save(file)

        execute()
        return redirect(url_for("process"))

    return render_template('index.html', user_form=form)

@app.route('/processing')
def process():
    return render_template('process.html')

#function that is being executed.
def execute():
    print('Execution started')
    for i in range(0,10):
        print(i)
    print('The execution was completed')

The process.html file is as follows: I want to display the terminal output on this page.

{% extends 'base.html' %}

{% block content %}
<p style="text-align:center; font-size:50px; color:#77C66E">File uploaded successfully</p>
{% endblock %}
  • 1
    Does this answer your question? [Streaming data with Python and Flask](https://stackoverflow.com/questions/13386681/streaming-data-with-python-and-flask) – JonSG Oct 27 '21 at 12:09
  • 1
    I understand that what you are trying to accomplish is a kind of background job done on the backend side, with feedback sent to the frontend. If that's it, then I recommend [this excellent tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxii-background-jobs) which explains very well how to do it. – Tobin Oct 27 '21 at 12:54

0 Answers0