How to show a pandas dataframe into a existing flask html table?

前端 未结 4 2021
暗喜
暗喜 2020-12-04 07:58

This may sound a noob question, but I\'m stuck with it as Python is not one of my best languages.

I have a html page with a table inside it, and I would like to show

4条回答
  •  暖寄归人
    2020-12-04 08:30

    # Declare table
    class SomeTable(Table):
        status = Col('Customer')
        city = Col('City')
        product_price = Col('Country')    
    
    # Convert the pandas Dataframe into dictionary structure
    output_dict = output.to_dict(orient='records')  
    
    # Populate the table
    table = SomeTable(output_dict)
    
    return (table.__html__())
    

    or as pandas return static HTML file you can render it as page using Flask

    @app.route('//')
    def render_static(filename):
        return render_template('%s.html' % filename)
    

    It's the Idea of how we can do it in Flask. Hope you can understand this and let me know if it's not helping!

    Update:

    import pandas as pd
    
    df = pd.DataFrame({'col1': ['abc', 'def', 'tre'],
                       'col2': ['foo', 'bar', 'stuff']})
    
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return df.to_html(header="true", table_id="table")
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    

    But I'd go with Flask HTML feature rather than DataFrame to HTML (due to styling)

提交回复
热议问题