Skip to content

Commit 931174f

Browse files
author
Chris
committed
replacing with py, clearing it up
1 parent cc0009a commit 931174f

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

5-advanced-prompts/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -573,20 +573,20 @@ Here are some good practices to consider:
573573

574574
## Assignment
575575

576-
Here's code in JavaScript showing how to build a simple API using Express.js:
576+
Here's code in Python showing how to build a simple API using Flask:
577577

578-
```javascript
579-
const express = require('express')
578+
```python
579+
from flask import Flask, request
580580

581-
const app = express()
581+
app = Flask(__name__)
582582

583-
app.get('/', (req, res) => {
584-
res.send('Hello World!')
585-
})
583+
@app.route('/')
584+
def hello():
585+
name = request.args.get('name', 'World')
586+
return f'Hello, {name}!'
586587

587-
app.listen(3000, () => {
588-
console.log('Example app listening on port 3000!')
589-
})
588+
if __name__ == '__main__':
589+
app.run()
590590
```
591591

592592
Use an AI assistant like GitHub Copilot or ChatGPT and apply the "self-refine" technique to improve the code.
@@ -598,7 +598,7 @@ Please attempt to solve the assignment by adding suitable prompts to the code.
598598
> [!TIP]
599599
> Phrase a prompt to ask it to improve, it's a good idea to limit how many improvements. You can also ask to improve it in a certain way, for example architecture, performance, security, etc.
600600

601-
[Solution](./solution.js)
601+
[Solution](./solution.py)
602602

603603
## Knowledge check
604604

5-advanced-prompts/assignment.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import Flask, request
2+
3+
app = Flask(__name__)
4+
5+
@app.route('/')
6+
def hello():
7+
name = request.args.get('name', 'World')
8+
return f'Hello, {name}!'
9+
10+
if __name__ == '__main__':
11+
app.run()
File renamed without changes.
File renamed without changes.

5-advanced-prompts/solution.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# here are some suggestions to improve the code:
2+
3+
# - 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.
4+
5+
# - 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.
6+
7+
# - 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.
8+
9+
from flask import Flask, request
10+
from flask_wtf import FlaskForm
11+
from wtforms import StringField, SubmitField
12+
from wtforms.validators import DataRequired, Length, Email
13+
14+
app = Flask(__name__)
15+
app.config['SECRET_KEY'] = 'secret_key'
16+
17+
class HelloForm(FlaskForm):
18+
name = StringField('Name', validators=[DataRequired(), Length(min=3)])
19+
email = StringField('Email', validators=[DataRequired(), Email()])
20+
submit = SubmitField('Submit')
21+
22+
@app.route('/', methods=['GET', 'POST'])
23+
def hello():
24+
form = HelloForm()
25+
if form.validate_on_submit():
26+
name = form.name.data
27+
email = form.email.data
28+
return f'Hello, {name} ({email})!'
29+
return form.render_template()
30+
31+
@app.errorhandler(400)
32+
def bad_request(error):
33+
return 'Bad request', 400
34+
35+
if __name__ == '__main__':
36+
app.run()

0 commit comments

Comments
 (0)