skip to Main Content

I am trying to display data from the SQL table onto the html page

My data is a MEDIUMBLOB type data

main.py

@app.route("/text")
def text():
    def generate():
        if 'name' in request.form:
            cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
            name = request.form['name']
            here = cursor.execute("SELECT transcript from `movies` WHERE movie_name = %s",[name])
            with open(here) as f:
                f.read()
    return app.response_class(generate(),mimetype='text/plain')

view.html

<div class="top">
<h1>APPLES</h1>
    </div>

{% block content %}
<div class="scontainer">
    <div id="list">
        <iframe src="/text" height="400" width="600"></iframe>
    </div>
</div>
{% endblock %}

</body>
</html>

I tried this but I could not get the results that I was looking for.
It just turns up blank like the image shown below
Problem image

2

Answers


  1. Just return the content of f.read() from your generate() function.

    Login or Signup to reply.
  2. I have updated the answer. Change your code as the following.

    from flask import Flask, render_template
    @app.route("/text") 
    def text():
        def generate():
            if 'name' in request.form:
                cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
                name = request.form['name']
                here = cursor.execute("SELECT transcript from `movies` WHERE movie_name = %s",[name])
            with open(here) as f:
                content = f.read()
            return content
        html_content = generate()
        with app.app_contenxt():
            return render_template("view.html",mimetype='text/plain', content=html_content)
    

    and your view.html:

    <div class="top">
    <h1>APPLES</h1>
        </div>
    
    {% block content %}
    <div class="scontainer">
        <div id="list">
            <iframe src="{{content}}" height="400" width="600"></iframe>
        </div>
    </div>
    {% endblock %}
    
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search