11 Questions
The app.route(...) used to define service routes is a decorator in python.
True
Which statement is NOT correct about the code below, based on our labs?
if name == 'main': app.run(port=5000, debug=True)
Because of 'debug=True', we need to manually restart the app (Ctrl+C and rerun the python module) whenever the code is changed while it is running.
In the lab code, app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root@localhost:3306/book' specifies a book database. Now let's assume the database is relocated on a server with the IP address 10.0.40.3. Which part of the URI do you need to replace with 10.0.40.3?
localhost
from flask import Flask, request, jsonify from flask_sqlalchemy import SQLAlchemy
app = Flask(name)
app.config['SQLALCHEMY_DATABASE_URI'] =
'mysql+mysqlconnector://jason:mary@localhost:3306/asset'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Asset(db.Model): tablename = 'asset'
asset_num = db.Column(db.String(32), primary_key=True)
name = db.Column(db.String(64), nullable=False)
description = db.Column(db.String(256), nullable=False)
owner_id = db.Column(db.Integer)
def __init__(self, asset_num, name, description, owner_id):
self.asset_num = asset_num
self.name = name
self.description = description
self.owner_id = owner_id
more codes below...
A, B, C
WampServer is also a web service.
False
The parameter used for app.route(...) should contain a complete URL including either a hostname or an IP, such as "http://127.0.0.1:5000/" or "http://127.0.0.1:5000/book/".
False
@app.route("/book/title/")
def find_by_title(title):
book = db.session.scalars(
db.select(Book).filter_by(title=title).
limit(1)
).first()
if book:
return jsonify(
{
"code": 216,
"data": "yes"
}
)
return jsonify(
{
"code": 404,
"message": "Dun tell you."
}
), 444
A & B
The Flask-SQLAlchemy python package is a service.
False
The postman app is a service consumer.
True
The book.py implements a REST web service.
True
from flask import Flask app = Flask(name)
@app.route("/") def www(): return "HELLO"
def home(): return "HI"
python -m flask run
Open URL http://127.0.0.1:5000/ in the browser will display "HELLO".
This quiz tests your understanding of replacing the database URI when relocating a database in a Flask application using SQLAlchemy. The code provided includes a Flask app with a defined SQLAlchemy database URI and requires identifying the part of the URI that needs to be replaced with the new server IP address.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free