# Similarities (more) Internal server error

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) + 1

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

# Cost of substitution
if s1[i - 1] == s2[j - 1]:
x = 0
else:
x = 1
sub = cost(i - 1, j - 1) + 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}
<th>{{c}}</th>
</tr>
</table>
{% endblock %}
``````
• 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. Jun 27, 2018 at 11:33

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.