Error during template rendering | Could not parse the remainder: '(pandas.NaT)' from 'pandas.isnull(pandas.NaT)'

huangapple go评论64阅读模式
英文:

Error during template rendering | Could not parse the remainder: '(pandas.NaT)' from 'pandas.isnull(pandas.NaT)'

问题

I can help with the translation. Here's the translated content:

我正在尝试在Django应用程序中通过HTML显示一个字典,其中字典的每个值都是一个数据框。

我能够打印出键,但无法打印出数据框,对此我尝试了许多不同的方法。

要么我打印出键值对,但这对人类来说不太可读,要么我会收到“无法解析剩余部分”的错误消息。

以下是views.py的代码片段:

context = {'rail': rail}

return render(request, 'sample_output.html', context)

其中rail是我的字典。

以下是HTML的代码片段:

<table>
    <thead>
        <tr>
            {% for key in rail.keys %}
            <th>{{ key }}</th>
            {% endfor %}
        </tr>
    </thead>
    <tbody style="height: 400px; overflow-y: scroll;">
        {% for key, value in rail.items %}
        <tr>
            <td>{{ key }}</td>
            <td>
                <ul>
                    {% for index, row in value.iterrows %}
                    <tr>
                        {% for cell in row %}
                          <td>
                            {% if cell is not pandas.isnull(pandas.NaT) %}
                                {{ cell }}
                            {% else %}
                                NaT
                            {% endif %}
                          </td>
                        {% endfor %}
                      </tr>
                      {% endfor %}
                </ul>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
英文:

I'm trying to display a dictionary via html in django app and that each value of dictionary is a dataframe.

I'm able to print key but not dataframe for which I've tried many different approaches.

Either I get key value pair printed which is not very human readable or I get Could not parse the remainder error

Here's snippet of views.py

            context = {&#39;rail&#39;: rail}

            return render(request, &#39;sample_output.html&#39;, context)

where rail is my dictionary

Here's snippet of html

    &lt;table&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                {% for key in rail.keys %}
                &lt;th&gt;{{ key }}&lt;/th&gt;
                {% endfor %}
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody style=&quot;height: 400px; overflow-y: scroll;&quot;&gt;
            {% for key, value in rail.items %}
            &lt;tr&gt;
                &lt;td&gt;{{ key }}&lt;/td&gt;
                &lt;td&gt;
                    &lt;ul&gt;
                        {% for index, row in value.iterrows %}
                        &lt;tr&gt;
                            {% for cell in row %}
                              &lt;td&gt;
                                {% if cell is not pandas.isnull(pandas.NaT) %}
                                    {{ cell }}
                                {% else %}
                                    NaT
                                {% endif %}
                              &lt;/td&gt;
                            {% endfor %}
                          &lt;/tr&gt;
                          {% endfor %}

                    &lt;/ul&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            {% endfor %}
   
         &lt;/tbody&gt;
    &lt;/table&gt;

答案1

得分: 1

Use to_html() with safe filter:

context = {'rail': {k: v.to_html() for k, v in rail.items()}}

For each dataframe, in your template use <td>{{ value|safe }}</td>

Example:

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
print(df.to_html())

# Output
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>col1</th>
      <th>col2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>3</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td>4</td>
    </tr>
  </tbody>
</table>
英文:

Use to_html() with safe filter:

context = {&#39;rail&#39;: {k: v.to_html() for k, v in rail.items()}}

For each dataframe, in your template use &lt;td&gt;{{ value|safe }}&lt;/td&gt;

Example:

df = pd.DataFrame({&#39;col1&#39;: [1, 2], &#39;col2&#39;: [3, 4]})
print(df.to_html())

# Output
&lt;table border=&quot;1&quot; class=&quot;dataframe&quot;&gt;
  &lt;thead&gt;
    &lt;tr style=&quot;text-align: right;&quot;&gt;
      &lt;th&gt;&lt;/th&gt;
      &lt;th&gt;col1&lt;/th&gt;
      &lt;th&gt;col2&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;0&lt;/th&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;3&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;1&lt;/th&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

huangapple
  • 本文由 发表于 2023年4月17日 03:08:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定