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

前端 未结 4 2029
暗喜
暗喜 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:23

    In case anyone finds this helpful. I have gone with an alternative because I needed more customization, including the ability to add buttons in the table that performed actions. I also really don't like the standard table formatting as it is very ugly IMHO.

    ...
    
    df = pd.DataFrame({'Patient Name': ["Some name", "Another name"],
                           "Patient ID": [123, 456],
                           "Misc Data Point": [8, 53]})
    ...
    
    # link_column is the column that I want to add a button to
    return render_template("patient_list.html", column_names=df.columns.values, row_data=list(df.values.tolist()),
                               link_column="Patient ID", zip=zip)
    

    HTML Code: This Dynamically Converts any DF into a customize-able HTML table

    
            {% for col in column_names %}
            
            {% endfor %}
        
        {% for row in row_data %}
        
            {% for col, row_ in zip(column_names, row) %}
            {% if col == link_column %}
            
            {% else %}
            
            {% endif %}
            {% endfor %}
        
        {% endfor %}
    
    
    {{col}}
    {{row_}}

    CSS Code

    table {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
    }
    
    td, th {
        border: 1px solid #ffffdffffd;
        text-align: left;
        padding: 8px;
    }
    
    tr:nth-child(even) {
        background-color: #ffffdffffd;
    }
    

    It performs very well and it looks WAY better than the .to_html output.

提交回复
热议问题