Missing columns in final html table after rendering the pandas dataframe to html

戏子无情 提交于 2021-01-29 10:33:35

问题


I am writing the below df values into a html template. I am missing chemistry and algebra column in the final output html table.

import pandas as pd
df = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
     'physics': [68, 74, 77, 78],
     'chemistry': [84, 56, 73, 69],
     'algebra': [78, 88, 82, 87]})
df_html = df.to_html()

    

  template_vars = { "html_table":df_html}

  f = open(output.html, 'w', encoding='utf-8')
  html_template ="metrics.html"
  temp = Template(open(html_template, 'r').read())
  template = temp.render(template_vars)
  f.write(template)
  f.close()

In the below Metrics.html has 4 buttons, out of which 2nd button is not showing up all the columns.

Metrics.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" 

href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <style>
        #incorrect-response-panel .panel{
            border-radius: 0;
        }

        .response-panel-title {
            font-weight: normal;
        }

        .doc-info h3 {
            font-weight: normal;
        }
    </style>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .collapsible {
      background-color: #777;
      color: white;
      cursor: pointer;
      padding: 18px;
      width: 100%;
      border: none;
      text-align: left;
      outline: none;
      font-size: 15px;
    }

    .active, .collapsible:hover {
      background-color: #555;
    }

    .collapsible:after {
      content: '\002B';
      color: white;
      font-weight: bold;
      float: right;
      margin-left: 5px;
    }

    .active:after {
      content: "\2212";
    }

    .content {
      padding: 0 18px;
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.2s ease-out;
      background-color: #f1f1f1;
    }
    </style>

</head>
<body>
 <button id=button1 type="button" class="collapsible">Top 1 Correct answer </button>
    <div class="content">
        {{correct_tab1}}
    </div>
    <div>
        </br>  </br>
    </div>
    <button id=button2  type="button" class="collapsible">Top 1 Incorrect answer </button>
    <div class="content">
        {{html_table}}
    </div>

    <div>
        </br>  </br>
    </div>
    <button id=button3  type="button" class="collapsible">Top 1 Correct answer </button>
    <div class="content">
        {{correct_tab1}}
    </div>

    <div>
        </br>  </br>
    </div>
    <button id=button4  type="button" class="collapsible">Top 1 Incorrect answer </button>
    <div class="content">
        {{incorrect_tab1}}
    </div>
    
    <div>
        </br>  </br>
    </div>

https://codepen.io/code_rev/pen/VwKBpRY


回答1:


As I testing your code partly after romoving () in (df=) and (`df_html=) in your qustion:

import pandas as pd
df = pd.DataFrame({'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
     'physics': [68, 74, 77, 78],
     'chemistry': [84, 56, 73, 69],
     'algebra': [78, 88, 82, 87]})
df_html = df.to_html()

we find the content of df_html is (just reformating):

<table border="1" class="dataframe">  
    <thead>    
    <tr style="text-align: right;">      
        <th></th>      
        <th>name</th>      
        <th>physics</th>      
        <th>chemistry</th>      
        <th>algebra</th>    
    </tr>  
    </thead>  
    <tbody>    
    <tr>      
        <th>0</th>      
        <td>Somu</td>      
        <td>68</td>      
        <td>84</td>      
        <td>78</td>    
    </tr>    
    <tr>      
        <th>1</th>      
        <td>Kiku</td>      
        <td>74</td>      
        <td>56</td>      
        <td>88</td>    
    </tr>    
    <tr>      
        <th>2</th>      
        <td>Amol</td>      
        <td>77</td>      
        <td>73</td>      
        <td>82</td>    
    </tr>    
    <tr>      
        <th>3</th>      
        <td>Lini</td>      
        <td>78</td>      
        <td>69</td>      
        <td>87</td>    
    </tr>  
    </tbody>
</table>

so we find the complete DataFrame with all the columns.

Hence, this part of the code is correct (after removing the character (`) I mentioned above).

The only suspicious sentence, in my thought, is

 template = temp.render(template_vars)

which depneds on html_template ="metrics.html" and template_vars = { "html_table":df_html}

Therefore, if there is a problem still existed, it would be necessary to add 'metrics.html`. we may find something.

Extending In the 'metrics.html` you uploaded, I have changed the following part:

    <button id=correct1 type="button" class="collapsible">Correct answer </button>
    <div class="content">
        {{correct_tab1}}
    </div>
    <div>
        </br>  </br>
    </div>
    <button id=incorrect1  type="button" 

to

    <button id=correct1 type="button" class="collapsible">Correct answer </button>
    <div class="content">
        {{html_table}} <!-- to match your question -->
    </div>
    <div>
        </br>  </br>
    </div>

then after running your code, the rendered part in output.html related to the table we intend is:

    <button id=incorrect1  type="button" class="collapsible"> Incorrect answer </button>
    <div class="content">
        <table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>name</th>
      <th>physics</th>
      <th>chemistry</th>
      <th>algebra</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Somu</td>
      <td>68</td>
      <td>84</td>
      <td>78</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Kiku</td>
      <td>74</td>
      <td>56</td>
      <td>88</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Amol</td>
      <td>77</td>
      <td>73</td>
      <td>82</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Lini</td>
      <td>78</td>
      <td>69</td>
      <td>87</td>
    </tr>
  </tbody>
</table>
    </div>

    <div>
        </br>  </br>
    </div>

so we notice that {{html_table}} we replaced correctly with HTML code of the table including all the columns.

so I do not find any problem.

I guess, you may investigate an old version of output.html and the generated one is in another folder.

or as it is clear, you did not upload the 'metrics.html` you work with, since the uploaded one does not contain the HTML you added in your question



来源:https://stackoverflow.com/questions/65605150/missing-columns-in-final-html-table-after-rendering-the-pandas-dataframe-to-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!