Python 在html.Div中以html格式写入数据

Python 在html.Div中以html格式写入数据,python,html,pandas,html-table,plotly-dash,Python,Html,Pandas,Html Table,Plotly Dash,我正在网页上抓取一个html表格,我能够阅读表格,现在我只需要写回我的页面。 所以我需要在html.Div()中返回它,并将其视为一个真实的表 def table(): URL = "https://www.infrontanalytics.com/fe-en/BMG5876H1051/Marvell-Technology-Group-Ltd-/beta" tables = pd.read_html(URL) print("There

我正在网页上抓取一个html表格,我能够阅读表格,现在我只需要写回我的页面。 所以我需要在html.Div()中返回它,并将其视为一个真实的表

def table():
URL = "https://www.infrontanalytics.com/fe-en/BMG5876H1051/Marvell-Technology-Group-Ltd-/beta"
        tables = pd.read_html(URL)
        print("There are : ",len(tables)," tables")
        print("Take look at table 0")
        print(tables[7])
        htmlout = html.Div(tables[7].to_html())
        return htmlout
我在网页上看到的结果是带有标记的书面表格,但我需要可视化表格:

<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>Company Name</th> <th>Ctry</th> <th>MarketCap.last (mUSD)</th> <th>Beta1-Year</th> <th>Year-To-DatePrice Change(in local currency)</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Marvell Technology Grou...</td> <td>BMU</td> <td>29 073</td> <td>1.40</td> <td>63.3%</td> </tr> <tr> <th>1</th> <td>International Peers Median</td> <td>International Peers Median</td> <td>International Peers Median</td> <td>1.26</td> <td>42.1%</td> </tr> <tr> <th>2</th> <td>NXP Semiconductors NV</td> <td>NLD</td> <td>45 876</td> <td>1.26</td> <td>28.9%</td> </tr> <tr> <th>3</th> <td>Silicon Motion Technolo...</td> <td>CYM</td> <td>1 561</td> <td>0.94</td> <td>-12.4%</td> </tr> <tr> <th>4</th> <td>QUALCOMM Inc.</td> <td>USA</td> <td>178 268</td> <td>1.20</td> <td>78.6%</td> </tr> <tr> <th>5</th> <td>STMicroelectronics NV</td> <td>NLD</td> <td>36 873</td> <td>1.48</td> <td>42.1%</td> </tr> <tr> <th>6</th> <td>NVIDIA Corp.</td> <td>USA</td> <td>335 702</td> <td>1.36</td> <td>130.5%</td> </tr> </tbody> </table>
公司名称Ctry MarketCap.last(mUSD)截至1年的价格变化(以当地货币表示)0 Marvell技术集团。。。BMU 29 073 1.40 63.3%1国际同行中位数国际同行中位数1.26 42.1%2 NXP半导体NV NLD 45 876 1.26 28.9%3硅运动技术。。。CYM 1 561 0.94-12.4%4美国高通公司178 268 1.20 78.6%5意法半导体公司NLD 36 873 1.48 42.1%6美国NVIDIA公司335 702 1.36 130.5%
如何将从pandas获得的标记转换为html格式,以在html页面中返回?
我使用了to_html()函数,但它没有像我预期的那样工作。

出于安全原因,您不能将原始html插入到破折号元素中。您可以选择手动将数据帧转换为包含
html.Tr
html.Td
等的
html.Table
元素,或者更好地使用

在您的情况下,这看起来像这样:

def table():
    URL = "https://www.infrontanalytics.com/fe-en/BMG5876H1051/Marvell-Technology-Group-Ltd-/beta"
    tables = pd.read_html(URL)
    print("There are : ",len(tables)," tables")
    print("Take look at table 0")
    print(tables[7])
    df = tables[7]
    htmlout = html.Div(dash_table.DataTable(
        id='table',
        columns=[{"name": i, "id": i} for i in df.columns],
        data=df.to_dict('records'),
    ))
    return htmlout
别忘了导入:

import dash_table

您好,如果您正在尝试使用Plotly,您应该查看本页:(部分“短划线中的表格”)。Plotly似乎不直接使用html,而是使用dict转换的数据帧。否则,Plotly将html呈现为纯文本。