Skip to main content

Python wrapper for html_transpose - a Rust library for transposing HTML tables

Project description

html_transpose

Python wrapper for html_transpose - a Rust library for transposing HTML tables while preserving merged cells, attributes, and structure.

Features

  • Table Transposition: Swaps rows and columns of HTML tables
  • Merged Cell Support: Correctly handles rowspan and colspan attributes
  • Attribute Preservation: Maintains all attributes on both table and cell elements
  • HTML Escaping: Properly escapes HTML special characters
  • Error Handling: Returns descriptive errors for invalid input
  • High Performance: Built with Rust for speed

Installation

Using uv

uv pip install html_transpose

Using pip

pip install html_transpose

From source

# Clone the repository
git clone https://github.com/snurf198/html_transpose_py.git
cd html_transpose_py

# Install using uv
uv pip install -e .

# Or using pip
pip install -e .

Usage

import html_transpose

# Simple table transpose
html_table = """
<table>
  <tr><td>A</td><td>B</td></tr>
  <tr><td>C</td><td>D</td></tr>
</table>
"""

transposed = html_transpose.transpose(html_table)
print(transposed)
# Output:
# <table>
#   <tr><td>A</td><td>C</td></tr>
#   <tr><td>B</td><td>D</td></tr>
# </table>

Handling Merged Cells

The library correctly handles tables with rowspan and colspan:

html_table = """
<table>
  <tr>
    <td rowspan="2">A</td>
    <td>B</td>
  </tr>
  <tr>
    <td>C</td>
  </tr>
</table>
"""

transposed = html_transpose.transpose(html_table)
# The rowspan becomes colspan in the transposed table

Error Handling

import html_transpose

try:
    result = html_transpose.transpose("<div>Not a table</div>")
except ValueError as e:
    print(f"Error: {e}")

API Reference

transpose(html: str) -> str

Transposes an HTML table string.

Parameters:

  • html (str): A string containing an HTML table (must contain a <table> element)

Returns:

  • str: The transposed HTML table as a string

Raises:

  • ValueError: If no <table> element is found in the input or if the HTML parser fails

Requirements

  • Python 3.8+
  • Rust (for building from source)

Development

Setting up the development environment

# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install dependencies
uv pip install -e ".[dev]"

# Or using pip
pip install -e ".[dev]"

Building

This package uses maturin to build the Rust extension:

# Install maturin
pip install maturin

# Build in development mode
maturin develop

# Build for distribution
maturin build

Running tests

# Using pytest
pytest

# Or using Python unittest
python -m unittest discover

License

This project is available under the MIT License.

Credits

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

html_transpose-0.0.2.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

html_transpose-0.0.2-cp314-cp314-macosx_11_0_arm64.whl (519.7 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

File details

Details for the file html_transpose-0.0.2.tar.gz.

File metadata

  • Download URL: html_transpose-0.0.2.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for html_transpose-0.0.2.tar.gz
Algorithm Hash digest
SHA256 48ba043323e81038458c508e77e1a775fdfe3f8b05f029fbde62049712ace643
MD5 3b19f0e13093c4b7d92b12d2f4c2cf3e
BLAKE2b-256 c6f6e91c0bed9c5cdd93323a1aa34c1e04b2488026b33de2c354cec2f9f30b75

See more details on using hashes here.

File details

Details for the file html_transpose-0.0.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for html_transpose-0.0.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 63bc8927abce8af32db4ed3b64d197fb750a4aa5247be4e47d28261ed28c428e
MD5 85a9a1158d00839132668ffbdc030d46
BLAKE2b-256 ac383895e4d4e16ac435f1a7b9011ffb48f0e622a2ac2e72a550f6c06501d049

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page