Skip to content

In this repository a function, "table_to_html", is defined that transforms any table into a HTML file.

Notifications You must be signed in to change notification settings

Denise-R/table_to_html

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 

Repository files navigation

Denise Rauschendorfer

10/17/2022

Table_to_HTML

In this repository a function, "table_to_html", is defined that transforms any table into a HTML file. This function was originally created to help my brother, James Rauschendorfer, upload supplemental information to a website for his PhD; however, this function may also be a useful way for others to publically share raw data and upload it to a website.

The following code was executed using Python 3.10.7.

Defining the function

Section 1:

In order to run the following code, the modules 're' and 'airium' need to be imported.

import re
import airium

Section 2:

To begin creating the function table_to_html, the source file needs to be transcribed into a list where each value (separated by commas/tabs/etc.) in the source file is a different element in the list.

Note: All of Sections 2-3 are contained within the 'def table_to_html(table_address, delineator, filename_html):'

To accomplish this, the following steps are taken:

  1. def is used to begin defining the function table_to_html with the variables table_address, delineator, and filename_html.
  2. The table_address is then opened as table in read (r) mode.
  3. An array/nested-list titled list is created.
  4. A for loop is created to iterate through each line in the table.
  5. Within this for loop, each line of table is divided into separate sections wherever there is a '\n' (new line character). This new list is then saved as x.
  6. The first element of x (element that does not include '\n') is then further divided wherever the delineator occurs, and saved as z.
  7. The values that were originally separated by the delineator in line of table (z) are then appended to list.
  8. Once this process has repeated for every line in table, the for loop is broken and the file table (file specified in table_address) is closed.
def table_to_html(table_address, delineator, filename_html):
    table = open(table_address, "r")
    list = []
    for line in table.readlines():
        x = re.split("\\n", line)
        y = str(x[0])
        z = re.split(delineator, y)
        list.append(z)
    table.close()

Section 3:

In this section, a HTML file whose title is specified in the variable filename_html is created that contains HTML code to represent the source table (table_address) as a website.

To accomplish this, the following steps are taken:

  1. The function Airium from the module airium is renamed a for convenience. Note: a will be used to easily transform python to HTML syntax.
  2. The filename_html is then opened in write (w) mode as f. Note: write (w) mode will cause the HTML file specified in the variable filename_html to be overwritten every time the function table_to_html is run with the same value for filename_html.
  3. The declaration <!DOCTYPE html> is added to a.
  4. Within the HTML <head> tag:
    • "utf-8" is defined as the character set.
    • The HTML contents are formatted to display based on the device width (i.e. phone, tablet, computer).
    • The title of the HTML is set to filename_html.
    • The table headers (<th>) and table data (<td>) are padded by 3 pixels.
    • The HTML function hover is used so that every table row (<tr>) the user's mouse hovers over in the HTML website is highlighted light-gray (represented by the hex code: #D3D3D3).
    • A 1 pixel black border is added around the entire <table>, each table header (<th>), and table data (<td>).
    • The function border-collapse is set to collapse so that there is no space between each cell of the table.
  5. A HTML <body> tag is created with the tags <table> and <tbody> nested inside of it.
  6. Within the HTML <tbody> tag:
    • A for loop is created to iterate through each element i in the array/nested-list, list, created in Section 2. Note: each element i in the array/nested-list, list, represents one row in the original source table.
    • Within this for loop, a table row is created using the HTML tag <tr>. Within the HTML <tr> tag:
      • A second for loop is created to iterate through each element j in i. Note: each element j in i represents the values within one row in the original source table.
      • An if/else statement is used so that the first row of list (i == 0) is nested within the HTML table header tag (<th>) and the background color for every element in that row is set to light grey (style="background-color:#D3D3D3;"). Every other row (i != 0) is nested within the HTML table data tag (<td>).
  7. Once this process has repeated for all the values j in every line i of the array/nested-list list, the for loops are broken.
  8. All of the HTML code that was added to a is then converted into a string titled html.
  9. html is appended to the file f (filename_html) and the file f is closed.
    a = airium.Airium()
    with open(filename_html, 'w') as f:
        a('<!DOCTYPE html>')
        with a.html(lang="pl"):
            with a.head():
                a.meta(charset="utf-8")
                a.meta(name="viewport", content="width=device-width, initial-scale=1.0")
                with a.title():
                    a(filename_html)
                with a.style():
                    a("th, td \n"
                    "\t\t{padding: 3px;}\n"
                    "\ttr:hover \n"
                    "\t\t{background-color: #D3D3D3;}\n"
                    "\ttable, th, td {\n"
                    "\t\tborder: 1px solid black;\n"
                    "\t\tborder-collapse: collapse;}")
            with a.body():
                with a.table():
                    with a.tbody():
                        for i in list:
                            index_i = list.index(i)
                            with a.tr():
                                for j in i:
                                    if index_i == 0:
                                        with a.th(style="background-color:#D3D3D3;"):
                                            a(j)
                                    else:
                                        with a.td():
                                            a(j)
        html = str(a)
        f.write(html)
    f.close()

Using the function

Section 4:

Now that the function table_to_html has been defined, it can be used by specifying the value of the variables table_address, delineator, and filename_html. When defining the table_address, each folder should be delineated by either "/", "//", or "\\" for table_address to be a valid entry in python.

Examples on how to use the function table_to_html with a CSV and TSV source file are shown below:

# TSV Source File
    table_address = "C:\\folder1\\folder2\\folder3\\folder4\\tsv_example.tsv"
    filename_html = "tsv_example.html"
    table_to_html(table_address, "\t", filename_html)

# CSV Source File
    table_address = "C:\\folder1\\folder2\\folder3\\folder4\\csv_example.csv"
    filename_html = "csv_example.html"
    table_to_html(table_address, ",", filename_html)

Appendix

Compiled Copy of Raw Code:

import re
import airium

def table_to_html(table_address, delineator, filename_html):
    table = open(table_address, "r")
    list = []
    for line in table.readlines():
        x = re.split("\\n", line)
        y = str(x[0])
        z = re.split(delineator, y)
        list.append(z)
    table.close()

    a = airium.Airium()
    with open(filename_html, 'w') as f:
        a('<!DOCTYPE html>')
        with a.html(lang="pl"):
            with a.head():
                a.meta(charset="utf-8")
                a.meta(name="viewport", content="width=device-width, initial-scale=1.0")
                with a.title():
                    a(filename_html)
                with a.style():
                    a("th, td \n"
                    "\t\t{padding: 3px;}\n"
                    "\ttr:hover \n"
                    "\t\t{background-color: #D3D3D3;}\n"
                    "\ttable, th, td {\n"
                    "\t\tborder: 1px solid black;\n"
                    "\t\tborder-collapse: collapse;}")
            with a.body():
                with a.table():
                    with a.tbody():
                        for i in list:
                            index_i = list.index(i)
                            with a.tr():
                                for j in i:
                                    if index_i == 0:
                                        with a.th(style="background-color:#D3D3D3;"):
                                            a(j)
                                    else:
                                        with a.td():
                                            a(j)
        html = str(a)
        f.write(html)
    f.close()


# To use the function:
    # TSV Source File
        table_address = "C:\\folder1\\folder2\\folder3\\folder4\\tsv_example.tsv"
        filename_html = "tsv_example.html"
        table_to_html(table_address, "\t", filename_html)

    # CSV Source File
        table_address = "C:\\folder1\\folder2\\folder3\\folder4\\csv_example.csv"
        filename_html = "csv_example.html"
        table_to_html(table_address, ",", filename_html)

About

In this repository a function, "table_to_html", is defined that transforms any table into a HTML file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published