Created
December 14, 2017 00:50
-
-
Save umarbrowser/048dc34e950425b39704a6814a909f50 to your computer and use it in GitHub Desktop.
This example prints a customized greeting that varies based on the path used to access the service using flask framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
# print a nice greeting. | |
def say_hello(username = "World"): | |
return '<p>Hello %s!</p>\n' % username | |
# some bits of text for the page. | |
header_text = ''' | |
<html>\n<head> <title>EB Flask Test</title> </head>\n<body>''' | |
instructions = ''' | |
<p><em>Hint</em>: This is a RESTful web service! Append a username | |
to the URL (for example: <code>/Thelonious</code>) to say hello to | |
someone specific.</p>\n''' | |
home_link = '<p><a href="/">Back</a></p>\n' | |
footer_text = '</body>\n</html>' | |
# EB looks for an 'application' callable by default. | |
application = Flask(__name__) | |
# add a rule for the index page. | |
application.add_url_rule('/', 'index', (lambda: header_text + | |
say_hello() + instructions + footer_text)) | |
# add a rule when the page is accessed with a name appended to the site | |
# URL. | |
application.add_url_rule('/<username>', 'hello', (lambda username: | |
header_text + say_hello(username) + home_link + footer_text)) | |
# run the app. | |
if __name__ == "__main__": | |
# Setting debug to True enables debug output. This line should be | |
# removed before deploying a production app. | |
application.debug = True | |
application.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment