Part 3: Visualizing the truth table

Would not it be nice to display the truth table on screen? That is what we will be doing in part 3. We will be creating a Python class for that goal. Furthermore, we will implement two ways to visualize the truth table. The first way is simply printing to the console and the other way is rendering the table in HTML.

The focus here is not to make an ASCII art generator or a html table generator. Hence, we will be using a library that can do this for us: PrettyTable. You give the data with some parameters, and it outputs the table for you.

The TruthTableVisualizer class

The data required is already contained in the TruthTable class. PrettyTable expects 3 main things: field names (a header), rows, and a title.

Some choices as to how we want the table to look are now discussed. We can choose to keep values aligned to the center with its align attribute. We can choose between displaying integers ($0, 1$) instead of booleans (true, false). The last choice is to print the table to the console or to generate a table in HTML.

class TruthTableVisualizer:
    def __init__(
        self, truth_table: TruthTable, as_int: bool = False
    ) -> None:
        self._table = PrettyTable(field_names=truth_table.header)
        self._table.title = (
            f"Truth table for wff: {self._table.field_names[-1]}"
        )
        self._table.align = "c"

        if as_int:
            self._table.add_rows(  
                [
                    [int(value) for value in row]
                    for row in truth_table.rows
                ]
            )
        else:
            self._table.add_rows(truth_table.rows)  

Next, we will implement the two visualization approaches.

Visualization approaches

With PrettyTable it is simple to print a table to the console. We can implement the __str__ method of TruthTableVisualizer using the get_string method of the PrettyTable object.

class TruthTableVisualizer:
 # ... other methods

  def __str__(self) -> str:
    return self._table.get_string()  

Here is an example generated from the formula $((A \land B)\lor C)$.

+-------------------------------------+
|  Truth table for wff: ((A ∧ B) ∨ C) |
+---+---+---+---------+---------------+
| A | B | C | (A ∧ B) | ((A ∧ B) ∨ C) |
+---+---+---+---------+---------------+
| 0 | 0 | 0 |    0    |       0       |
| 0 | 0 | 1 |    0    |       1       |
| 0 | 1 | 0 |    0    |       0       |
| 0 | 1 | 1 |    0    |       1       |
| 1 | 0 | 0 |    0    |       0       |
| 1 | 0 | 1 |    0    |       1       |
| 1 | 1 | 0 |    1    |       1       |
| 1 | 1 | 1 |    1    |       1       |
+---+---+---+---------+---------------+

Render in HTML

Rendering an HTML table involves generating the correct HTML and saving it to a file. First, we get the HTML table by using the get_html_string method of the PrettyTable object with format attribute to true, so that center alignment is taken into account. Next, we build a minimal HTML page, which can optionally include CSS styling to beautify the table.

<html>
    <head>
        <style>
          thead {{border-bottom:1px solid black}}
        </style>
    </head>
    <body>
        <!-- The table goes here -->
    </body>
</html>

Feel free to expand upon this basic example if you have a good understanding of HTML and CSS; you can enhance the design and functionality as needed.

Then, we give a filename as a parameter to our method, which we can name as save_as_html. After that, add the HTML table to the HTML page we created before. Finally, save the HTML file.

class TruthTableVisualizer:
  # ... other methods

  def save_as_html(self, filename: str) -> None:
    table_html = self._table.get_html_string(format=True)  
    with open(filename, mode="w", encoding="utf-8") as file:
      file.write(
        f"""<html>
      <head>
        <style>
          thead {{border-bottom:1px solid black}}
        </style>
      </head>
      <body>
        {table_html} 
      </body>
      </html>
      """
      )

Congratulations on reaching this point! Now, let’s move on to the conclusion.