如何通过循环文件名数组来扩展 HTML 表格?

问题描述 投票:0回答:1

我对 HTML 很陌生。我知道如何添加表格行和单元格,以及如何通过路径插入图像。现在我已经通过 Python 和 matplotlib 生成了一堆图形图像文件,并将它们分类到两个各自的目录中,我想在表格中描绘它们。基本上每个单元格一张图像,每列一个目录,以便它们并排对齐。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>graphs</title>
</head>
<body>
<table>
    <tr>
        <th>"heading1</th>
        <th>heading2</th>
    </tr>
    <tr>
        <tc> image1_1</tc>
        <tc> image2_1</tc>
    </tr>
    .
    .
    .
</table>
</body>
</html>
python html for-loop html-table
1个回答
0
投票

使用 ,如果您想使用

Styler
/
to_html
:

import pandas as pd
from itertools import groupby
from pathlib import Path

(
    pd.DataFrame(
        {
            d: list(fp) for d,fp in groupby(
                Path("plots").rglob("*.png"),
                key=lambda p: p.parent.name
            )
        }
    ).map('<img src="{}" width="70%" height="70%">'.format)
        .style.hide(axis=0)
        .set_table_styles(
            [
                dict(selector="*", props=[
                    ("text-align", "center"), ("width", "500px")])]
        )
    .to_html("table.html")
)

输出(table.html):

使用的输入:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
data = np.random.randn(100)

def mkplot(data, filename, color, plot_type):
    plt.figure(figsize=(10, 5))    
    if plot_type == "LP":
        plt.plot(data, color)
    elif plot_type == "SP":
        plt.scatter(range(100), data, color=color)
    elif plot_type == "HI":
        plt.hist(data, bins=20, color=color)
    elif plot_type == "BP":
        plt.bar(range(10), data[:10], color=color)
    
    plt.savefig(filename, dpi=300)

mkplot(data, "dir1/1.png", "lightcoral", "LP")
mkplot(data, "dir1/2.png", "blue", "SP")
mkplot(data, "dir2/3.png", "lightgreen", "HI")
mkplot(data, "dir2/4.png", "black", "BP")
© www.soinside.com 2019 - 2024. All rights reserved.