0

I am fairly new to Flask and am currently working on a project whose goal is to transcribe mp3 files into JSON. I decided to attempt to use Flask, but it's been more challenging than I thought.

As of right now, I am able to display a example JSON file in one of my html pages, but I have not been able to format it. I looked at some previous answers that told me to use jsonify, but it hasn't worked apparently. If you guys could give me a hand, any kind of comment would be really apreciated. Here is my code:

from flask import Flask, render_template, url_for, request, redirect, json, jsonify
import json
import os
from pathlib import Path

app = Flask(__name__)

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

@app.route('/upload', methods=['POST'])
def upload():
    file = request.files['inputFile']
    if Path(file.filename).suffix == '.mp3':
        filename = os.path.join(app.static_folder, 'data', 'json_test.json')
        with open(filename) as json_test:
            data = json.load(json_test)
        return render_template('index2.html', data=data)
    else:
        return render_template('erro.html')

if __name__ == "__main__":
    app.run(debug=True)
pebato
  • 1
  • What do you mean by "formatting"? How do you want to format it and how is it actually getting formatted? – ForceBru Oct 19 '20 at 18:36
  • There's a lot missing from this question to properly answer it, but try doing `return jsonify(json_test)` and see if that is what you were expecting. – noslenkwah Oct 19 '20 at 18:39
  • @ForceBru basically, just display it nicely on the .html page. cause the way it is now, it generates a 'one-line text box' with a scroll bar. – pebato Oct 19 '20 at 18:56
  • @pebato, this is the whole point: it returns the most dense representation possible. It's not meant for "nice-looking" rendering out of the box. You'll need to create your own markup and CSS to do that. – ForceBru Oct 19 '20 at 18:58
  • @noslenkwah I tried this before, I keep "TypeError: Object of type TextIOWrapper is not JSON serializable" – pebato Oct 19 '20 at 19:01
  • @ForceBru oh, i see. so what is the point of the JSONIFY_PRETTYPRINT_REGULAR or the json.dumps functions? I'm sorry if this sounds really dumb, I'm really new to Flask (like, 2 days old) – pebato Oct 19 '20 at 19:03
  • @pebato - see [this](https://stackoverflow.com/questions/44035799/cant-read-json-file-with-python-getting-type-error-json-object-is-textiowrap) question on solving the `TextIOWrapper` problem. This might actually be the same problem causing your current solution to fail. jinja2 templating silently renders nothing if an exception is found. – noslenkwah Oct 19 '20 at 22:06

0 Answers0