Sai A Sai A
Updated date Feb 15, 2024
In this blog, we will learn how to convert NumPy arrays into HTML format using Python. This blog explores multiple methods, including Pandas, BeautifulSoup, and NumPy's own functions, offering code examples and explanations for each approach.

Introduction:

In data manipulation and visualization in Python, NumPy stands as a powerful library. Its arrays offer efficient storage and operations for numerical data. But what if we want to showcase our NumPy arrays on the web? One elegant solution is to convert them into HTML format. In this blog, we will explore various methods to achieve this conversion, providing code snippets, explanations, and outputs along the way.

Method 1: Using Pandas

Pandas, a popular library built on top of NumPy, offers straightforward functionalities for data manipulation and conversion. We can leverage its DataFrame to convert a NumPy array to HTML effortlessly.

import pandas as pd
import numpy as np

# Sample NumPy array
data = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

# Convert to DataFrame
df = pd.DataFrame(data)

# Convert DataFrame to HTML
html_output = df.to_html()

print(html_output)

Output:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <th>1</th>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
    <tr>
      <th>2</th>
      <td>7</td>
      <td>8</td>
      <td>9</td>
    </tr>
  </tbody>
</table>

Pandas' DataFrame class provides a convenient method to_html() that converts the DataFrame into an HTML table format. By default, it includes necessary tags for styling and structure.

Method 2: Using BeautifulSoup

Another approach involves using BeautifulSoup, a library primarily designed for web scraping but handy for HTML manipulation. We can generate HTML markup manually using this library.

from bs4 import BeautifulSoup

# Sample NumPy array
data = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

# Generate HTML
html_output = '<table>'
for row in data:
    html_output += '<tr>'
    for col in row:
        html_output += f'<td>{col}</td>'
    html_output += '</tr>'
html_output += '</table>'

print(html_output)

Output:

<table>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>4</td><td>5</td><td>6</td></tr>
  <tr><td>7</td><td>8</td><td>9</td></tr>
</table>

With BeautifulSoup, we construct HTML markup iteratively, looping through the NumPy array's rows and columns. We manually create <table>, <tr>, and <td> tags to represent the structure of the array as an HTML table.

Method 3: Using NumPy's array_str Function

NumPy itself provides a function array_str that converts arrays to strings, which we can then embed within HTML.

# Sample NumPy array
data = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

# Convert array to string
array_str = np.array_str(data)

# Generate HTML
html_output = f'<pre>{array_str}</pre>'

print(html_output)

Output:

<pre>[[1 2 3]
 [4 5 6]
 [7 8 9]]</pre>

Here, we utilize NumPy's array_str function to convert the NumPy array into a string representation. We then enclose this string within <pre> tags in HTML to preserve the formatting.

Conclusion:

In this blog, we have explored various methods to convert NumPy arrays to HTML format in Python. Each method offers its own advantages, whether through the Pandas, the flexibility of BeautifulSoup, or the directness of NumPy's functions. 

Comments (0)

There are no comments. Be the first to comment!!!