I'm trying to create a dictionary using Python-Flask-HTML that loops through every single character received from the HTML input form and return its corresponding values an an output. For example:
input: Z.123/46X/78 ; output: Y-BCD+EFW+HI
Python-Flask code:
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class mydict(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Value %r>' % self.id
@app.route('/', methods=['POST','GET'])
def dictionary():
result = ''
definition = {'0': 'A', '1': 'B', '2': 'C', '3': 'D', '4': 'E', '5': 'F', '6': 'G', '7': 'H', '8': 'I', '9': 'J',
'Z': 'Y', 'X': 'W', '/': '+', '.': '-'}
if request.method == 'POST':
dict_content = request.form['content']
for num in dict_content:
result += definition[num]
new_content = mydict(content=result)
try:
db.session.add(new_content)
db.session.commit()
return redirect('/')
except:
return 'error '
else:
new_content = mydict.query.order_by(mydict.date_created).all()
return render_template('index.html', new_content=new_content)
@app.route('/delete/<int:id>')
def delete(id):
value_to_delete = mydict.query.get_or_404(id)
try:
db.session.delete(value_to_delete)
db.session.commit()
return redirect('/')
except:
return 'there was a prob deleting the value'
db.create_all()
if __name__ == "__main__":
app.run(debug=True)
Dictionary HTML code:
{% extends 'base.html' %}
{% block head %}
<title>Dictionary</title>
{% endblock %}
{% block body %}
<div class="content">
<h1 style="text-align: center">Dictionary</h1>
<table>
<tr>
<th>Definition</th>
<th>Added</th>
<th>Actions</th>
</tr>
{% for Value in new_content %}
<tr>
<td>{{ Value.content }}</td>
<td>{{ Value.date_created.date() }}</td>
<td>
<a href="/delete/{{Value.id}}">Delete</a>
</td>
{% endfor %}
</tr>
</table>
<div class="form">
<form action="/" method="POST">
<input pattern="[A-Z./]+" type="number" name="content" id="content">
<input type="submit" value="Enter Num">
</form>
</div>
</div>
{% endblock %}
Base HTML code:
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
{% block head %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
With the above code, I'm able to enter multiple integers and the output will only be a single letter. If I take out the dictionary portion of the code in the Python app and run it in Pycharm without the rest of the code, then I can get the output that I desire.
Moreover, is there a way to get the HTML input to accept a combination of numbers, letters and special characters? I'm currently using input pattern="[A-Z./]+" type="number" , but this doesn't seem to work. Your help will be greatly appreciated.
