使用html/css或任何python库来绘制服务器机架

发布于 2025-02-04 18:46:13 字数 1551 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2025-02-11 18:46:13

我不知道您需要什么 - 图像PNG/JPG,图像SVG,文本表,HTML表。

但是首先,它需要读取数据并

rows = [
   [1, 'empty']
   [2, 'firewall']
   [3, 'firewall']
   [4, 'firewall']
   [5, 'empty']

   # ... etc... 
]

在此列表中将其转换为这样的列表,它可以更简单地计算图像上文本或生成文本表或生成HTML的位置。

您可以使用使用它来生成文本表
您可以在< pre></pre>< code>>/code>

+------+----------+
|   Nr | Device   |
+======+==========+
|    1 | ?        |
+------+----------+
|    2 | firewall |
+------+----------+
|    3 | firewall |
+------+----------+
|    4 | firewall |
+------+----------+
|    5 | ?        |
+------+----------+
|    6 | ?        |
+------+----------+
|    7 | ?        |
+------+----------+
|    8 | ?        |
+------+----------+
|    9 | ?        |
+------+----------+
|   10 | NAS      |
+------+----------+
|   11 | NAS      |
+------+----------+
|   12 | NAS      |
+------+----------+
|   13 | NAS      |
+------+----------+
|   14 | NAS      |
+------+----------+
|   15 | NAS      |
+------+----------+
|   16 | servers1 |
+------+----------+
|   17 | servers1 |
+------+----------+
|   18 | ?        |
+------+----------+
|   19 | ?        |
+------+----------+
|   20 | server2  |
+------+----------+
|   21 | server2  |
+------+----------+
|   22 | server2  |
+------+----------+
|   23 | server2  |
+------+----------+
|   24 | server2  |
+------+----------+
|   25 | server2  |
+------+----------+

最小的文本工作代码

text = '''servers1 - 15 - 17
server2 - 20 - 25
firewall - 2 - 4
NAS - 10 - 15'''

import io

# --- read to dictionary ---

data = {}
counter = {}

with io.StringIO(text) as fh:
    for line in fh:
        line = line.strip()
        name, start, end = line.split(' - ')
        start = int(start)
        end = int(end)

        # - count -
        counter[name] = end-start+1
        
        for x in range(start, end+1):
            data[x] = name

# - display counter -
#print(counter)
for name, number in counter.items():
    print(f'{name:10}: {number}')
            
# --- convert to list ---

rows = []
last = max(data.keys())

for x in range(1, last+1):
    if x in data:
        name = data[x]
    else:
        name = '?'
    rows.append([x, name])
    
#print(rows)

# --- display table ---

import tabulate

print(tabulate.tabulate(rows, headers=['Nr', 'Device'], tablefmt='grid'))

# - count -
import collections
counter = collections.Counter(data.values())

# - display counter -
#print(counter)
for name, number in counter.items():
    print(f'{name:10}: {number}')

编辑 中 code

如果将pandas.dataframe中放置,则可以使用.to_html()将其生成为&lt ; table></table>

,但您也可以使用也可以生成< table>>手动

table = "<table>\n"

table += "  <tr>\n    <th>Nr</th>\n    <th>Device</th>\n  </tr>\n"

for number, device in rows:
    table += f"  <tr>\n    <td>{number}</td>\n    <td>{device}</td>\n  </th>\n"

table += "</table>\n"

print(table)

结果:

<table>
  <tr>
    <th>Nr</th>
    <th>Device</th>
  </tr>
  <tr>
    <td>1</td>
    <td>?</td>
  </th>
  <tr>
    <td>2</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>3</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>4</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>5</td>
    <td>?</td>
  </th>
  <tr>
    <td>6</td>
    <td>?</td>
  </th>
  <tr>
    <td>7</td>
    <td>?</td>
  </th>
  <tr>
    <td>8</td>
    <td>?</td>
  </th>
  <tr>
    <td>9</td>
    <td>?</td>
  </th>
  <tr>
    <td>10</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>11</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>12</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>13</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>14</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>15</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>16</td>
    <td>servers1</td>
  </th>
  <tr>
    <td>17</td>
    <td>servers1</td>
  </th>
  <tr>
    <td>18</td>
    <td>?</td>
  </th>
  <tr>
    <td>19</td>
    <td>?</td>
  </th>
  <tr>
    <td>20</td>
    <td>server2</td>
  </th>
  <tr>
    <td>21</td>
    <td>server2</td>
  </th>
  <tr>
    <td>22</td>
    <td>server2</td>
  </th>
  <tr>
    <td>23</td>
    <td>server2</td>
  </th>
  <tr>
    <td>24</td>
    <td>server2</td>
  </th>
  <tr>
    <td>25</td>
    <td>server2</td>
  </th>
</table>

编辑:

如果您使用flask,则可以将发送到模板

return render_template('template.html', rows=rows)

并直接在模板中运行循环

<table>
  <tr>
     <th>Nr</th>
     <th>Device</th>
  </tr>

{% for number, device in rows %}
  <tr>
     <td>{{ number }}</td>
     <td>{{ device }}</td>
  </tr>
{% endfor %}  

</table>

I don't know what you need - image PNG/JPG, image SVG, text table, HTML table.

But first it would need to read data and convert to list like this

rows = [
   [1, 'empty']
   [2, 'firewall']
   [3, 'firewall']
   [4, 'firewall']
   [5, 'empty']

   # ... etc... 
]

With this list it could be simpler to calculate positions for text on image, or generate text table, or generate HTML.

You could use it with tabulate to generate text table
which you can display in HTML in <pre></pre> or <code></code>

+------+----------+
|   Nr | Device   |
+======+==========+
|    1 | ?        |
+------+----------+
|    2 | firewall |
+------+----------+
|    3 | firewall |
+------+----------+
|    4 | firewall |
+------+----------+
|    5 | ?        |
+------+----------+
|    6 | ?        |
+------+----------+
|    7 | ?        |
+------+----------+
|    8 | ?        |
+------+----------+
|    9 | ?        |
+------+----------+
|   10 | NAS      |
+------+----------+
|   11 | NAS      |
+------+----------+
|   12 | NAS      |
+------+----------+
|   13 | NAS      |
+------+----------+
|   14 | NAS      |
+------+----------+
|   15 | NAS      |
+------+----------+
|   16 | servers1 |
+------+----------+
|   17 | servers1 |
+------+----------+
|   18 | ?        |
+------+----------+
|   19 | ?        |
+------+----------+
|   20 | server2  |
+------+----------+
|   21 | server2  |
+------+----------+
|   22 | server2  |
+------+----------+
|   23 | server2  |
+------+----------+
|   24 | server2  |
+------+----------+
|   25 | server2  |
+------+----------+

Minimal working code for text table

text = '''servers1 - 15 - 17
server2 - 20 - 25
firewall - 2 - 4
NAS - 10 - 15'''

import io

# --- read to dictionary ---

data = {}
counter = {}

with io.StringIO(text) as fh:
    for line in fh:
        line = line.strip()
        name, start, end = line.split(' - ')
        start = int(start)
        end = int(end)

        # - count -
        counter[name] = end-start+1
        
        for x in range(start, end+1):
            data[x] = name

# - display counter -
#print(counter)
for name, number in counter.items():
    print(f'{name:10}: {number}')
            
# --- convert to list ---

rows = []
last = max(data.keys())

for x in range(1, last+1):
    if x in data:
        name = data[x]
    else:
        name = '?'
    rows.append([x, name])
    
#print(rows)

# --- display table ---

import tabulate

print(tabulate.tabulate(rows, headers=['Nr', 'Device'], tablefmt='grid'))

# - count -
import collections
counter = collections.Counter(data.values())

# - display counter -
#print(counter)
for name, number in counter.items():
    print(f'{name:10}: {number}')

EDIT:

If you put rows in pandas.DataFrame then you can use .to_html() to generate it as <table></table>

But you can use rows also to generate <table> manually

table = "<table>\n"

table += "  <tr>\n    <th>Nr</th>\n    <th>Device</th>\n  </tr>\n"

for number, device in rows:
    table += f"  <tr>\n    <td>{number}</td>\n    <td>{device}</td>\n  </th>\n"

table += "</table>\n"

print(table)

Result:

<table>
  <tr>
    <th>Nr</th>
    <th>Device</th>
  </tr>
  <tr>
    <td>1</td>
    <td>?</td>
  </th>
  <tr>
    <td>2</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>3</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>4</td>
    <td>firewall</td>
  </th>
  <tr>
    <td>5</td>
    <td>?</td>
  </th>
  <tr>
    <td>6</td>
    <td>?</td>
  </th>
  <tr>
    <td>7</td>
    <td>?</td>
  </th>
  <tr>
    <td>8</td>
    <td>?</td>
  </th>
  <tr>
    <td>9</td>
    <td>?</td>
  </th>
  <tr>
    <td>10</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>11</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>12</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>13</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>14</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>15</td>
    <td>NAS</td>
  </th>
  <tr>
    <td>16</td>
    <td>servers1</td>
  </th>
  <tr>
    <td>17</td>
    <td>servers1</td>
  </th>
  <tr>
    <td>18</td>
    <td>?</td>
  </th>
  <tr>
    <td>19</td>
    <td>?</td>
  </th>
  <tr>
    <td>20</td>
    <td>server2</td>
  </th>
  <tr>
    <td>21</td>
    <td>server2</td>
  </th>
  <tr>
    <td>22</td>
    <td>server2</td>
  </th>
  <tr>
    <td>23</td>
    <td>server2</td>
  </th>
  <tr>
    <td>24</td>
    <td>server2</td>
  </th>
  <tr>
    <td>25</td>
    <td>server2</td>
  </th>
</table>

EDIT:

If you use flask then you can send rows to template

return render_template('template.html', rows=rows)

and run loop directly in template

<table>
  <tr>
     <th>Nr</th>
     <th>Device</th>
  </tr>

{% for number, device in rows %}
  <tr>
     <td>{{ number }}</td>
     <td>{{ device }}</td>
  </tr>
{% endfor %}  

</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文