2

Im new to building APIs using Flask and as a reference for my new area of knowledge i used this stackoverflow post: ([How to show a pandas dataframe into a existing flask html table?).

I have made the example work and added some other things in addition to the base functionality, but one things I haven't been able to make work is how I send in a parameter to subset the DataFrame. This would be something in the API call like: http://localhost/id_for_subset which will show a subset of the DataFrame and not the entire one like I have now.

I pasted the code from the question above for quick reference:

python code:

from flask import Flask, request, render_template, session, redirect
import numpy as np
import pandas as pd


app = Flask(__name__)

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c--', 'd', 'e']})


@app.route('/', methods=("POST", "GET"))
def html_table():

    return render_template('simple.html',  tables=[df.to_html(classes='data')], titles=df.columns.values)



if __name__ == '__main__':
    app.run(host='0.0.0.0')

Any form of help on this final bit is very much appreciated!

/swepab

1 Answer 1

0

You just need to query your dataframe before making the table.

#not sure why you need POST but Okay
@app.route('/<id>',methods=("POST", "GET"))
def page(id):
    subset_id = id
    df_filtered = df.loc[df["<id_column_name>"]==subset_id]
    return render_template('simple.html', tables=[df_filtered.to_html(classes='data')], titles=df.columns.values)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.