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 %}
flask run
show when you trigger that error? The error message (including the stack trace) might point you to what happens.