json2html is a Python library that fetches data from APIs, allows users to define the structure of the JSON response, and generates responsive HTML pages to display the data in a user-friendly format.
Project description
Json2HTML 
json2html is a Python library that fetches data from APIs, allows users to define the structure of the JSON response, and generates responsive HTML pages to display the data in a user-friendly format.
classes
ApiHandler
This class is responsible for managing communication with the API and
retrieving JSON data.
It takes the API URL as input during initialization.
The fetch_data method sends a GET request to the API and returns the JSON
data.
It includes error handling for timeouts, connection errors, HTTP errors, and
invalid JSON responses.
JsonDefinition
This class is used to define the structure of the JSON data and how each
field should be displayed.
It uses an Enum called DisplayType to represent the different display types
(text, image, video, etc.).
The add_field method allows you to add a field definition, specifying the
field name, display type, and any additional options.
It includes validation to ensure that required options are provided for
certain display types (e.g., width and height for images).
HtmlGenerator
This class generates the HTML code based on the JSON data and definitions
provided.
It uses the BeautifulSoup library to create and manipulate HTML elements.
The generate_html method iterates over the data and definitions, calling the
appropriate method to generate the HTML for each field.
It includes methods for adding the HTML head (with title and CSS links), and
for generating the HTML for different field types (text, image, video, file,
link).
It uses a templating engine (Jinja2) to separate the HTML code from the
Python code, making it easier to maintain and modify the HTML structure.
example.py
You can use this example code!
from json2html import ApiHandler, HtmlGenerator, JsonDefinition, DisplayType
# API address
api_url = "https://dog.ceo/api/breeds/image/random"
api_handler = ApiHandler(api_url)
data = api_handler.fetch_data()
if data:
json_definition = JsonDefinition()
json_definition.add_field("message", DisplayType.IMAGE)
json_definition.add_field("status", DisplayType.TEXT)
html_generator = HtmlGenerator(data, json_definition)
html = html_generator.generate_html()
html_generator.save_html("dog_image.html")
print("HTML generated successfully!")
else:
print("Failed to fetch data from API.")```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json-into-html-0.1.0.tar.gz.
File metadata
- Download URL: json-into-html-0.1.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3e92394947f33b5c30f0bf01d5bc68062f393f81e690606f4a3f02d8c777c8b
|
|
| MD5 |
59122cbfbb423398e58297448b0e98de
|
|
| BLAKE2b-256 |
7aec3817a2ebe58df6b2070dd62b0fcc6bd6f8df05cf49fde1af586af36e6ae5
|
File details
Details for the file json_into_html-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json_into_html-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3a7dc756a5118acac04f86afa092c2942ecf50537ebd98922cffb7afd0c6507
|
|
| MD5 |
3979aa8cad3e21d2cb62b4064f0623e1
|
|
| BLAKE2b-256 |
9a88a18456b88a969b5d7f6103201392517bf57b88970f7dbfe752ff795abed1
|