Skip to main content

Tools to handle the CRUD of .html files as objects.

Project description

Coverage Status

HTML 2 Object

Tools to handle the CRUD of .html files as objects.

Install

pip install html2object

Functions

  • Parse your html file

    html = open(<path>, "r").read("bin/gui/index.html")
    document = HtmlElement(html)
    
  • Add attributes

    document.add_attribute("class", "flex pt-2")
    
  • Add children

    p = HtmlElement(name="p").add_child("This is a text")
    document.add_child(p)
    
  • Set new children

    p = HtmlElement(name="p").add_child("This is a text")
    document.set_children([p])
    
  • Get element by id

    image = document.get_element_by_id("image_id")
    image.add_attribute("src", "image_url")
    
  • Get elements by name

    images = document.get_elements_by_name("image")
    for image in images:
      url_list.append(image.get_attribute("src"))
    
  • Get elements by class name

    theme_element = document.get_elements_by_class_name("radix-themes")
    for element in theme_element:
      print(f"Main color: {element.get_attribute('data-accent-color')}")
    

Usage

This project provides a way to manipulate HTML files and update them dynamically. Here's how you can use it:

First, import the necessary classes from this project:

from html2object import HtmlElement

read your html file and:

Create an HtmlElement()

html = open(<path>, "r").read("bin/gui/index.html")
document = HtmlElement(html)

Create scripts

update_script = HtmlElement(name="script").add_child(
    "setInterval(() => reload(), 1500);"
)
document.add_child(update_script)

Create a div

p = HtmlElement(name="p", id="textID").add_chil("Text")
div = HtmlElement(name="div").add_child(p)
document.add_child(div)

Use Document functions like JS

text_element = document.get_element_by_id("textID")
strong = HtmlElement(name="strong").add_child("Strong text")
text_element.set_child([strong, "No strong text"])

save the html file running

open(path, "w").write(str(document)).close()

Local setup

Instructions on how to install and set up your project. Include any dependencies that need to be installed.

  1. Clone the repository
  2. Navigate to the project directory:
cd html2object
  1. Create a virtual environment (optional but recommended):
python3 -m venv venv
  1. Activate the virtual environment:
  • For Windows:
venv\Scripts\activate
  • For macOS and Linux:
source venv/bin/activate
  1. Install the project dependencies:
pip install -r requirements.txt

That's it! Your project should now be installed and ready to use.

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

html2object-1.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

html2object-1.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file html2object-1.1.0.tar.gz.

File metadata

  • Download URL: html2object-1.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for html2object-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9db40abc2c4beaddfa43cc1a2b12bceaea6fd8b4827e95ea8dd3a5726f4b86ee
MD5 de4ae482450084a8f483e9cca70c2393
BLAKE2b-256 f528f22104f1d02a40ad4862b956b4a82fed9ec64f4e59760b2ea1101a095a3b

See more details on using hashes here.

File details

Details for the file html2object-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: html2object-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for html2object-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a1dd262b69196d1c7e973075c8701bc98cd857bea4c90ee664bcb1724fc61a
MD5 ef689c0435aca3bb05e31aae16d7952e
BLAKE2b-256 c4752db7cc4afb5222694dbbaa8e417f7b88023c0d5232a9da9f9da013c8b1a5

See more details on using hashes here.

Supported by

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