0

I get a 500 Internal server error whenever I press the score button to go to 'score.html'. My helper.py code works fine. I think the problem is with my Matrix.html page. Please help.

Helpers.py:

from enum import Enum


class Operation(Enum):
    """Operations"""

    DELETED = 1
    INSERTED = 2
    SUBSTITUTED = 3

    def __str__(self):
        return str(self.name.lower())

# Declaring cost matrix
cost_matrix = []

def set_matrix(n,m):
    """To set cost matrix"""

    # Setting rows of matrix
    for i in range(n):
        cost_matrix.append([])

        # Setting columns of matrix
        for j in range(m):
            if i == 0 and j == 0:
                cost_matrix[i].append((0, None))
            elif i == 0:
                cost_matrix[i].append((j, Operation.INSERTED))
            elif j == 0:
                cost_matrix[i].append((i, Operation.DELETED))
            else:
                cost_matrix[i].append((None, None))

# Declaring input strings
s1 = "haha"
s2 = "haha"

def distances(a, b):
    """Calculate edit distance from a to b"""

    # Setting value of input strings
    global s1
    global s2
    s1 = a
    s2 = b
    n = len(a) + 1
    m = len(b) + 1

    # Setting cost matrix
    set_matrix(n,m)

    # Computing costs of each cell
    for r in range(1,n):
        for c in range(1,m):
            cost_matrix[r][c] = cost(r,c)
    return cost_matrix


def cost(i,j):
    """Recursively computes cost"""

    # Compute cost
    if cost_matrix[i][j] == (None,None):
        # Cost of deletion
        dele = cost(i - 1, j)[0] + 1

        # Cost of insertion
        ins = cost(i, j - 1)[0] + 1

        # Cost of substitution
        if s1[i - 1] == s2[j - 1]:
            x = 0
        else:
            x = 1
        sub = cost(i - 1, j - 1)[0] + x

        # Filling cost of current cell
        min_cost = min(dele, ins, sub)
        if min_cost == dele:
            return (dele, Operation.DELETED)
        elif min_cost == ins:
            return (ins , Operation.INSERTED)
        else:
            return (sub, Operation.SUBSTITUTED)

    else:
        return cost_matrix[i][j]

index.html:

{% extends "layout.html" %}

{% block body %}

    <h2>Edit Distance</h2>
    <form action="/score" method="POST">
        <div class="form-group">
            <label>String 1</label>
                <input name="string1" type="text" class="form-control" placeholder="First string"/>
        </div>
         <div class="form-group">
            <label>String 2</label>
                <input name="string2" type="text" class="form-control"placeholder="Second string"/>
        </div>
        <input value="Score" type="submit" class="btn btn-primary">
    </form>
{% endblock %}

matrix.html (not complete):

{% extends "layout.html" %}

{% block body %}
    <table class="table">
        <tr>
            <th></th>
            <th></th>
           {% for c in s2}
               <thead>
                   <th>{{c}}</th>
               </thead>
        </tr>
    </table>
{% endblock %}
1
  • What does the terminal where you typed flask run show when you trigger that error? The error message (including the stack trace) might point you to what happens.
    – Blauelf
    Commented Jun 27, 2018 at 11:33

1 Answer 1

1

Take a quick look at score.html, don't you notice that at line 6, it already includes matrix.html into the page? Notice that it also includes log.html at line 11?

The point that I'm making is that, you don't need to include {% extends "layout.html" %} and {% block body %} with its associated ending tag anymore in matrix.html, in fact, this is what causes your internal server error!

Since these statements are already included in score.html, and matrix.html is actually just a part of the whole of the aforementioned page, with log.html being the second half, the score page is basically a webpage that already takes the basic layout with 'sub-webpages' inside of it! That's just a neat little feature that Jinja gives you.

Now, why do those tags give you the error? Basically because you're taking the layout and putting the same thing inside of it! No need to go full on low-level explanation, you get the point.

That's why the problem specification tells you to at least take a quick read through the entire distribution code always! Even though you won't understand everything, you would notice little things like this! Remember, even the little things can cause big errors when programming! Always keep a keen eye! :)

2
  • thanks for helping out. Commented Jun 28, 2018 at 5:38
  • No problem! Would you kindly mark the answer then by clicking that big check icon next to my answer? Helps to keep the forum clean! Commented Jun 28, 2018 at 7:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .