forked from microsoft/generative-ai-for-beginners
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoai-solution.py
36 lines (27 loc) · 1.44 KB
/
aoai-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.
fromflaskimportFlask, request
fromflask_wtfimportFlaskForm
fromwtformsimportStringField, SubmitField
fromwtforms.validatorsimportDataRequired, Length, Email
app=Flask(__name__)
app.config['SECRET_KEY'] ='secret_key'
classHelloForm(FlaskForm):
name=StringField('Name', validators=[DataRequired(), Length(min=3)])
email=StringField('Email', validators=[DataRequired(), Email()])
submit=SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
defhello():
form=HelloForm()
ifform.validate_on_submit():
name=form.name.data
email=form.email.data
returnf'Hello, {name} ({email})!'
returnform.render_template()
@app.errorhandler(400)
defbad_request(error):
return'Bad request', 400
if__name__=='__main__':
app.run()