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

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

Accessing HTML table row element from pandas dataframe

问题

以下是代码的翻译部分:

def generate_html_main_page(dataframe: pd.DataFrame):
    # 从数据框中获取表格的HTML
    table_html = dataframe.to_html(table_id="table")
    # 构建包含jQuery数据表格的完整HTML
    html = f"""
    <html>
    <header>
        <link href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css" rel="stylesheet">
    </header>
    <body>
    {table_html}
    <script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI" crossorigin="anonymous"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
    var array = document.getElementById("table").rows;
    for (let i = 0; i < array.length; ++i) {
        var elem = array[i].cells[1].innerHTML;
        document.getElementById("table").rows[i].cells[1].innerHTML = "<a href='#test'>" + elem + "</a>";
        document.write(document.getElementById("table").rows[i].cells[1].innerHTML);
    }
    </script>
    </body>
    </html>
    """
    # 返回HTML
    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.

  File &quot;&lt;fstring&gt;&quot;, line 2
    var elem = array[i].cells[1].innerHTML;
           ^
SyntaxError: invalid syntax

def generate_html_main_page(dataframe: pd.DataFrame):
    # get the table HTML from the dataframe
    table_html = dataframe.to_html(table_id=&quot;table&quot;)
    # construct the complete HTML with jQuery Data tables
    html = f&quot;&quot;&quot;
    &lt;html&gt;
    &lt;header&gt;
        &lt;link href=&quot;https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;/header&gt;
    &lt;body&gt;
    {table_html}
    &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;
    &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;
    &lt;script type=&quot;text/javascript&quot;&gt;
    var array = document.getElementById(&quot;table&quot;).rows
	for (let i = 0; i &lt; array.length; ++i) {
        	var elem = array[i].cells[1].innerHTML;
        	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;
        	document.write(document.getElementById(&quot;table&quot;).rows[i].cells[1].innerHTML)
	}
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    &quot;&quot;&quot;
    # return the html
    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:

确定