3

I'm trying to decode an output from my database from base64 to text. here is a snippit my code:

from flask import Flask,render_template, request, json
from flask_mysqldb import MySQL
import base64

app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'example'
app.config['MYSQL_PASSWORD'] = 'example'
app.config['MYSQL_DB'] = 'products_db'

mysql = MySQL(app)

@app.route("/")
def main():
    cur = mysql.connection.cursor()
    cur.execute("select * from products;", mysql.connection.commit())
    data = cur.fetchall()
    return render_template('products.html', data=data)

my html page:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<table border = 1>
    {% for product in data %}
    <tr>
        <td> {{product[0]}}</td>
        <td> {{product[2]}}</td>
        <td> {{base64.b64decode(product[3])}}</td>
    </tr>
    {% endfor %}
</table>
  </body>
</html>

I'm trying to decode product[3] but im failing, how to do it on flask? how can i import other modules aswell?

thank you

xiholac761
  • 51
  • 7
  • @CDJB I'm new to FLASK and do not understand how to implement that solution to my code. I'm still learning how FLASK works – xiholac761 Nov 15 '19 at 16:26

2 Answers2

1

You can apply the function to the target data value before passing the returned database contents to render_template:

@app.route("/")
def main():
   cur = mysql.connection.cursor()
   cur.execute("select * from products;", mysql.connection.commit())
   data = [[*a, base64.b64decode(b)] for *a, b in cur.fetchall()]
   return render_template('products.html', data=data)

Now, in your template:

{% for product in data %}
  <tr>
    <td> {{product[0]}}</td>
    <td> {{product[2]}}</td>
    <td> {{product[3]}}</td>
  </tr>
{% endfor %}

Edit: Python2 solution:

data = [i[:3]+[base64.b64decode(i[3])]+i[4:] for i in cur.fetchall()]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Thank you. it is giving me an error regarding the star before the character "a" in the data variable. – xiholac761 Nov 15 '19 at 16:41
  • @xiholac761 No problem, please see my recent edit. `*` can be used for unpacking, and is available in Python3 only. I added a solution for Python2. – Ajax1234 Nov 15 '19 at 16:42
  • thank you so much. flask has thrown me an error: ValueError: too many values to unpack could it be because too many letter? what do these letters represent if i may ask? – xiholac761 Nov 15 '19 at 16:46
  • @xiholac761 The use of multiple comma separate variables in a `for` loop is called unpacking. I have updated the Python2 solution to better reflect your database content structure. – Ajax1234 Nov 15 '19 at 16:51
1

You can't do base64 decode using jinja templating language. base64.b64decode is not supported in jinja. You may do this on server side and pass the data to template.

from flask import Flask,render_template, request, json
from flask_mysqldb import MySQL
import base64

app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'example'
app.config['MYSQL_PASSWORD'] = 'example'
app.config['MYSQL_DB'] = 'products_db'

mysql = MySQL(app)

@app.route("/")
def main():
    cur = mysql.connection.cursor()
    cur.execute("select * from products;", mysql.connection.commit())
    data = cur.fetchall()
    modified_data = []
    for item in data:
        modified_item = []
        modified_item.append(item[0])
        modified_item.append(item[1])
        modified_item.append(base64.b64decode(item[3])
        modified_data.append(modified_item)
    return render_template('products.html', data=modified_data)

template index.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<table border = 1>
    {% for item in data %}
    <tr>
        <td> {{item[0]}}</td>
        <td> {{item[2]}}</td>
        <td> {{item[3]}}</td>
    </tr>
    {% endfor %}
</table>
  </body>
</html>
Srikanth Chekuri
  • 1,944
  • 1
  • 9
  • 19