0
0
Flaskframework~10 mins

Service layer pattern in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a service class method that returns a greeting message.

Flask
class GreetingService:
    def get_greeting(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aprint("Hello")
B"Hello from service!"
Creturn "Hi"
DHello
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of return
Not using quotes around the string
2fill in blank
medium

Complete the Flask route to call the service method and return its result.

Flask
from flask import Flask
app = Flask(__name__)

service = GreetingService()

@app.route('/')
def home():
    return [1]
Drag options to blanks, or click blank then click option'
Aservice.get_greeting
BGreetingService.get_greeting()
Cservice.get_greeting()
Dget_greeting()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the method itself without calling it
Calling the method on the class instead of the instance
3fill in blank
hard

Fix the error in the service method to accept a name parameter and return a personalized greeting.

Flask
class GreetingService:
    def get_greeting(self, [1]):
        return f"Hello, {name}!"
Drag options to blanks, or click blank then click option'
Aname
Bself
Cusername
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one used in the string
Forgetting to add the parameter
4fill in blank
hard

Fill both blanks to update the Flask route to accept a name URL parameter and pass it to the service.

Flask
from flask import Flask, request
app = Flask(__name__)

service = GreetingService()

@app.route('/hello/<[1]>')
def hello():
    name = request.view_args.get('[2]')
    return service.get_greeting(name)
Drag options to blanks, or click blank then click option'
Aname
Buser
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the URL parameter and the key in view_args
Forgetting to include the URL parameter in the route
5fill in blank
hard

Fill all three blanks to create a service method that validates input and returns a message accordingly.

Flask
class GreetingService:
    def get_greeting(self, name):
        if not name or len(name) [1] 0:
            return [2]
        return f"Hello, {name}[3]"
Drag options to blanks, or click blank then click option'
A<=
B"Hello, Guest!"
C!"
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator
Not returning a default message for empty name
Forgetting punctuation in the greeting