0

This question is not a duplicate of Passing HTML to template using Flask/Jinja2. It deals with dataframes and is specific to a layout. Please read it carefully.

I have created a file load interface for uploading training and test data. Now I want, on the right portion of the page just opposite to the corresponding button, that data.head() should be printed, to get an idea of the structure of the data. This should happen after the /upload route is called.

I am aware of the df.to_html() option, but I'm not able to think where to incorporate it in the code.

Code used is:

from flask import Flask, render_template, request
from werkzeug import secure_filename

app = Flask(__name__)

@app.route('/upload', endpoint = 'upload_file1')

def upload_file1():
    return render_template('view.html')

@app.route('/upload', endpoint = 'upload_file3')

def upload_file3():
    return render_template('view.html')
@app.route('/uploader', methods = ['GET', 'POST'], endpoint= 'upload_file2')
def upload_file2():
   if request.method == 'POST':
      f = request.files['file']
      f.save(secure_filename(f.filename))
      return 'file uploaded successfully'

if __name__ == '__main__':
   app.run(debug = True, use_reloader=False)
robinCTS
  • 5,746
  • 14
  • 30
  • 37
user2906657
  • 521
  • 2
  • 4
  • 18
  • Why there are two renderers written for /upload? And what is the error you are getting? – Reck Mar 24 '18 at 07:04
  • Because I want to create 2 buttons one for training data and one for test. Code runs fine. Now as I said, want to display the respective data.head() in front of the buttons. Just wanted some help how to proceed? – user2906657 Mar 24 '18 at 07:16

1 Answers1

1

You can pass params to render_template and render using the params.

@app.route('/update', endpoint = 'upload_file1')
def upload_file1():
    df_table = df.head().html()
    return render_template('view.html', df_table=df_table)

view.html

{% block content %}
  {{df_table | safe}}
{% endblock %}
Reck
  • 1,388
  • 11
  • 20