Skip to main content

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

json-into-html MIT License

json-2-html 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 json-into-html 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


Download files

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

Source Distribution

json-into-html-0.1.1.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

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

json_into_html-0.1.1-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

File details

Details for the file json-into-html-0.1.1.tar.gz.

File metadata

  • Download URL: json-into-html-0.1.1.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

Hashes for json-into-html-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b3fbe5ccebbc6fde9edfc23885e520af9658e7430d9ee7c4a45f0304b00ee861
MD5 ae69d63425687b6e9015eed6eaaee652
BLAKE2b-256 f642d21bf2a4922acdd1a3ec350fd21b9753e997c865b82511ec266b0e9f27a3

See more details on using hashes here.

File details

Details for the file json_into_html-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: json_into_html-0.1.1-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

Hashes for json_into_html-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d508c7cbed6e614f8a3b29d4e85d9a736ecb68ea6079b1ac9f1b44aca4b82526
MD5 020795e2ec8775f27578596848904440
BLAKE2b-256 e41fa2aae0057299108a60f90c8a6f6228f50c14e8dbbe929b6d1d2e09a3c108

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