forked from microsoft/generative-ai-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
36 lines (27 loc) · 1.44 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# here are some suggestions to improve the code:
# - Add input validation to prevent malicious input from being processed by the server. You can use a library like flask-wtf to validate user input and sanitize it before processing.
# - Use environment variables to store sensitive information such as database credentials, API keys, and other secrets. This will prevent the information from being hard-coded in the code and exposed in case of a security breach.
# - Implement error handling to provide meaningful error messages to the user in case of errors. You can use the @app.errorhandler() decorator to handle exceptions and return an error response.
from flask import Flask, request
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length, Email
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
class HelloForm(FlaskForm):
name = StringField('Name', validators=[DataRequired(), Length(min=3)])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def hello():
form = HelloForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
return f'Hello, {name} ({email})!'
return form.render_template()
@app.errorhandler(400)
def bad_request(error):
return 'Bad request', 400
if __name__ == '__main__':
app.run()