> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encord.com/llms.txt
> Use this file to discover all available pages before exploring further.

# HTML

## Import Text Regions to HTML

A single `text region` object label can be added at a time between `start` and `end` using `range`.

<CodeGroup>
  ```python Template theme={"dark"}

  # Import dependencies
  from typing import List
  from encord import EncordUserClient
  from encord.objects import LabelRowV2, Object, ObjectInstance, OntologyStructure
  from encord.objects.coordinates import HtmlCoordinates
  from encord.objects.html_node import HtmlRange, HtmlNode

  SSH_PATH = "<file-path-to-ssh-private-key>"
  PROJECT_ID = "<project-unique-id>"

  # Instantiate Encord client
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify the project
  project = user_client.get_project(PROJECT_ID)

  label_row = project.list_label_rows_v2(
      data_title_eq="<file-name-of-html-file>"
  )[0]

  label_row.initialise_labels()

  range_html = HtmlRange(
      start=HtmlNode(
         xpath="<full-XPath>",
          offset=<location-of-text-or-object>
      ),
      end=HtmlNode(
         xpath="<full-XPath>",
          offset=<location-of-text-or-object>
      )
  )

  html_object: Object = project.ontology_structure.get_child_by_title(
          title="<text-region-name>", type_=Object
      )

  html_object_instance: ObjectInstance = html_object.create_instance()

  # `frames=0` is an optional. It indicates that the text file is a single data unit.

  html_object_instance.set_for_frames(frames=0, coordinates=HtmlCoordinates(range=[range_html]))

  label_row.add_object_instance(html_object_instance)
  label_row.save()

  ```

  ```python Example theme={"dark"}

  # Import dependencies
  from typing import List
  from encord import EncordUserClient
  from encord.objects import LabelRowV2, Object, ObjectInstance, OntologyStructure
  from encord.objects.coordinates import HtmlCoordinates
  from encord.objects.html_node import HtmlRange, HtmlNode

  SSH_PATH = "/Users/chris-encord/sdk-ssh-private-key.txt"
  PROJECT_ID = "7b345a15-5f76-4a12-961b-2b1f2vv932bb"

  # Instantiate Encord client
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify the project
  project = user_client.get_project(PROJECT_ID)

  label_row = project.list_label_rows_v2(
      data_title_eq="My HTML Page.html"
  )[0]

  label_row.initialise_labels()

  range_html = HtmlRange(
      start=HtmlNode(
         xpath="/html/body/div[1]/main/div/div[2]/div[2]/div/div[1]/div[2]/p[3]",
          offset=30
      ),
      end=HtmlNode(
         xpath="/html/body/div[1]/main/div/div[2]/div[2]/div/div[1]/div[2]/p[3]",
          offset=40
      )
  )

  html_object: Object = project.ontology_structure.get_child_by_title(
          title="Text Region - HTML", type_=Object
      )

  html_object_instance: ObjectInstance = html_object.create_instance()

  # `frames=0` is an optional. It indicates that the text file is a single data unit.

  html_object_instance.set_for_frames(frames=0, coordinates=HtmlCoordinates(range=[range_html]))

  label_row.add_object_instance(html_object_instance)
  label_row.save()

  ```
</CodeGroup>

## Import Classifications to HTML

The example for the Classification uses nested attributes with the Ontology structure as follows:

* Accurate?
  * Yes
  * No
    * Correction (text field to provide edits for the correction)

<Note>`create_instance` must use `range_only=True` for text documents. This includes HTML documents.</Note>

<CodeGroup>
  ```python Template theme={"dark"}

  # Import dependencies
  from typing import List
  from pathlib import Path
  from encord import EncordUserClient, Project
  from encord.objects import LabelRowV2, Classification, Option, OntologyStructure

  SSH_PATH = "<file-path-to-ssh-private-key>"
  PROJECT_ID = "<project-unique-id>"

  # Create user client using access key
  user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
      Path(SSH_PATH).read_text()
  )

  # Get project for which predictions are to be added
  project: Project = user_client.get_project(PROJECT_ID)

  # Specify the data unit to apply classification
  label_row = project.list_label_rows_v2(
      data_title_eq="<file-name-for-html-file>.html"
  )[0]


  # Download the existing labels 
  label_row.initialise_labels()

  # Get the Ontology structure
  ontology_structure: OntologyStructure = label_row.ontology_structure

  # Assume that the following radio button classification exists in the Ontology.
  radio_ontology_classification: Classification = (
      ontology_structure.get_child_by_title(
          title="<classification-name>", type_=Classification
      )
  )

  radio_classification_option = radio_ontology_classification.get_child_by_title(
  title="<option-name>",
  type_=Option
  )

  # Create classification instance. `range_only=True` is required for HTML documents
  radio_classification_instance = radio_ontology_classification.create_instance(range_only=True)

  # Set the answer of the classification instance
  radio_classification_instance.set_answer(radio_classification_option)

  # Select the frames where the classification instance is present. Not required for global classifications
  radio_classification_instance.set_for_frames(frames=0)

  # Add it to the label row
  label_row.add_classification_instance(radio_classification_instance)

  # Save labels
  label_row.save()

  ```

  ```python Example theme={"dark"}

  # Import dependencies
  from __future__ import annotations
  from pathlib import Path
  from encord import EncordUserClient, Project
  from encord.objects import (
      Classification,
      OntologyStructure,
      LabelRowV2,
      TextAttribute,
      RadioAttribute,
      Option
  )
  from encord.objects.html_node import HtmlRange, HtmlNode

  SSH_PATH = "/Users/chris-encord/sdk-ssh-private-key.txt"
  PROJECT_ID = "8a321a14-5f76-4a12-961b-3b1f2da932gk"

  # Create user client using access key
  user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
      Path(SSH_PATH).read_text()
  )

  # Get project for which predictions are to be added
  project: Project = user_client.get_project(PROJECT_ID)

  # Specify the data unit to apply classification
  label_row = project.list_label_rows_v2(
      data_title_eq="My HTML Page.html"
  )[0]

  label_row.initialise_labels()

  ontology_structure: OntologyStructure = label_row.ontology_structure

  # Get the parent classification
  text_ontology_classification = ontology_structure.get_child_by_title(
      title="Accurate?", type_=Classification
  )

  # Create an instance of the radio button classification
  text_classification_instance = text_ontology_classification.create_instance(range_only=True)

  # Get the radio button attribute and option
  accurate_radio_attribute = text_ontology_classification.get_child_by_title("Accurate?", type_=RadioAttribute)
  not_accurate_option = text_ontology_classification.get_child_by_title(title="No", type_=Option)

  # Set the answer for the radio button
  text_classification_instance.set_answer(attribute=accurate_radio_attribute, answer=not_accurate_option)

  # Get the nested attribute "Correction"
  correction_attribute = text_ontology_classification.get_child_by_title("Correction", type_=TextAttribute)

  # Set the text for the nested attribute
  text_classification_instance.set_answer(attribute=correction_attribute, answer="This page needs to be updated.")

  # Set the classification (for text documents) for use with the entire file
  text_classification_instance.set_for_frames(frames=0)

  # Add the classification instance to the label row
  label_row.add_classification_instance(text_classification_instance)

  # Save the label row
  label_row.save()

  ```
</CodeGroup>

## Export Labels for HTML Files

<CodeGroup>
  ```python Template theme={"dark"}

  # Import dependencies
  from encord import EncordUserClient
  import json

  SSH_PATH= "<file-path-to-ssh-private-key"
  PROJECT_ID= "<project-unique-id>"
  DATA_UNIT_NAME= "<file-name-of-html-file>"

  # Instantiate client. Replace <private_key_path> with the path to the file containing your private key.
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify Project. Replace <project_hash> with the hash of the Project you want to export labels for.
  project = user_client.get_project(PROJECT_ID)

  # Specify the data unit you want to export labels for. Replace <file_name> with the name of your specific data unit.
  specific_label_row = project.list_label_rows_v2(
      data_title_eq=DATA_UNIT_NAME
  )[0]

  # Download label information for the specific data unit
  specific_label_row.initialise_labels()

  # Print the labels as JSON
  print(json.dumps(specific_label_row.to_encord_dict()))

  ```

  ```python Example theme={"dark"}

  # Import dependencies
  from encord import EncordUserClient
  import json

  SSH_PATH= "/Users/chris-encord/sdk-ssh-private-key.txt"
  PROJECT_ID= "7a321a15-5f76-4a12-961b-2b1f2da932bb"
  DATA_UNIT_NAME= "My HTML Page.html"

  # Instantiate client. Replace <private_key_path> with the path to the file containing your private key.
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify Project. Replace <project_hash> with the hash of the Project you want to export labels for.
  project = user_client.get_project(PROJECT_ID)

  # Specify the data unit you want to export labels for. Replace <file_name> with the name of your specific data unit.
  specific_label_row = project.list_label_rows_v2(
      data_title_eq=DATA_UNIT_NAME
  )[0]

  # Download label information for the specific data unit
  specific_label_row.initialise_labels()

  # Print the labels as JSON
  print(json.dumps(specific_label_row.to_encord_dict()))

  ```
</CodeGroup>

## Remove Labels from HTML Files

<CodeGroup>
  ```python Template theme={"dark"}

  from encord import EncordUserClient
  import json

  SSH_PATH= "<file-path-to-ssh-private-key>"
  PROJECT_ID= "<project-unique-id>"
  DATA_UNIT_NAME= "<file-name-of-html-file>"

  # Instantiate client. Replace <private_key_path> with the path to the file containing your private key.
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify Project. Replace <project_hash> with the hash of the Project you want to export labels for.
  project = user_client.get_project(PROJECT_ID)

  # Specify the data unit you want to export labels for. Replace <file_name> with the name of your specific data unit.
  specific_label_row = project.list_label_rows_v2(
      data_title_eq=DATA_UNIT_NAME
  )[0]


  object_to_remove = None
  specific_label_row.initialise_labels()
  for object_instance in specific_label_row.get_object_instances():
      if object_instance.object_hash == '<label-unique-id>':
          object_to_remove = object_instance

  specific_label_row.remove_object(object_to_remove)

  specific_label_row.save()

  ```

  ```python Example theme={"dark"}

  from encord import EncordUserClient
  import json

  SSH_PATH= "/Users/chris-encord/sdk-ssh-private-key.txt"
  PROJECT_ID= "7v321a15-5f76-4a12-961b-2b1f2da932bb"
  DATA_UNIT_NAME= "My HTML Page.html"

  # Instantiate client. Replace <private_key_path> with the path to the file containing your private key.
  user_client = EncordUserClient.create_with_ssh_private_key(
      ssh_private_key_path=SSH_PATH
  )

  # Specify Project. Replace <project_hash> with the hash of the Project you want to export labels for.
  project = user_client.get_project(PROJECT_ID)

  # Specify the data unit you want to export labels for. Replace <file_name> with the name of your specific data unit.
  specific_label_row = project.list_label_rows_v2(
      data_title_eq=DATA_UNIT_NAME
  )[0]


  object_to_remove = None
  specific_label_row.initialise_labels()
  for object_instance in specific_label_row.get_object_instances():
      if object_instance.object_hash == 'dH3DrglO':
          object_to_remove = object_instance

  specific_label_row.remove_object(object_to_remove)

  specific_label_row.save()

  ```
</CodeGroup>
