从 Pandas 数据框中访问 HTML 表格行元素

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

Accessing HTML table row element from pandas dataframe

问题

以下是代码的翻译部分:

  1. def generate_html_main_page(dataframe: pd.DataFrame):
  2. # 从数据框中获取表格的HTML
  3. table_html = dataframe.to_html(table_id="table")
  4. # 构建包含jQuery数据表格的完整HTML
  5. html = f"""
  6. <html>
  7. <header>
  8. <link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet">
  9. </header>
  10. <body>
  11. {table_html}
  12. <script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI" crossorigin="anonymous"></script>
  13. <script type="text/javascript" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
  14. <script type="text/javascript">
  15. var array = document.getElementById("table").rows;
  16. for (let i = 0; i < array.length; ++i) {
  17. var elem = array[i].cells[1].innerHTML;
  18. document.getElementById("table").rows[i].cells[1].innerHTML = "<a href='#test'>" + elem + "</a>";
  19. document.write(document.getElementById("table").rows[i].cells[1].innerHTML);
  20. }
  21. </script>
  22. </body>
  23. </html>
  24. """
  25. # 返回HTML
  26. return html

希望这对你有帮助!

英文:

I have the following code to generate html from pandas dataframe. I'm using JS to access each table row but getting an error.

  1. File &quot;&lt;fstring&gt;&quot;, line 2
  2. var elem = array[i].cells[1].innerHTML;
  3. ^
  4. SyntaxError: invalid syntax
  1. def generate_html_main_page(dataframe: pd.DataFrame):
  2. # get the table HTML from the dataframe
  3. table_html = dataframe.to_html(table_id=&quot;table&quot;)
  4. # construct the complete HTML with jQuery Data tables
  5. html = f&quot;&quot;&quot;
  6. &lt;html&gt;
  7. &lt;header&gt;
  8. &lt;link href=&quot;https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css&quot; rel=&quot;stylesheet&quot;&gt;
  9. &lt;/header&gt;
  10. &lt;body&gt;
  11. {table_html}
  12. &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.slim.min.js&quot; integrity=&quot;sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  13. &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
  14. &lt;script type=&quot;text/javascript&quot;&gt;
  15. var array = document.getElementById(&quot;table&quot;).rows
  16. for (let i = 0; i &lt; array.length; ++i) {
  17. var elem = array[i].cells[1].innerHTML;
  18. document.getElementById(&quot;table&quot;).rows[i].cells[1].innerHTML = &quot;&lt;a href=&#39;#test&#39;&gt;&quot; + elem +&quot;&lt;/a&gt;&quot;
  19. document.write(document.getElementById(&quot;table&quot;).rows[i].cells[1].innerHTML)
  20. }
  21. &lt;/script&gt;
  22. &lt;/body&gt;
  23. &lt;/html&gt;
  24. &quot;&quot;&quot;
  25. # return the html
  26. return html

Any pointers are greatly appreciated. Thanks!

答案1

得分: 0

您正在使用 f-string 生成HTML,但HTML中还包含{/}字符。因此,Python尝试将您的JavaScript for 循环的主体作为Python代码执行。

当您希望它们只是字面大括号时,您可以将它们转义为{{/}}。或者使用普通的字符串连接(html = &#39;&#39;&#39;...&#39;&#39;&#39; + table_html + &#39;&#39;&#39;...&#39;&#39;&#39;)。

英文:

You're using an f-string to generate HTML, but the HTML also has {/} characters in it. So Python is trying to execute the body of your JavaScript for loop as Python code.

You can escape them as {{/}} when you want them to just be literal braces. Or use plain string concatenation (html = &#39;&#39;&#39;...&#39;&#39;&#39; + table_html + &#39;&#39;&#39;...&#39;&#39;&#39;).

huangapple
  • 本文由 发表于 2023年2月24日 02:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548738.html
匿名

发表评论

匿名网友

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

确定