From 1dd6e841c2149d03059cd0baf613f67cc38d8858 Mon Sep 17 00:00:00 2001 From: sohanvir Date: Tue, 29 Aug 2017 21:08:25 +0530 Subject: [PATCH 1/2] Basic flask web program --- flask-dev/flask-web-dev/README.md | 13 +++++++++ flask-dev/flask-web-dev/chap02/basic-web.py | 30 +++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 flask-dev/flask-web-dev/README.md create mode 100644 flask-dev/flask-web-dev/chap02/basic-web.py diff --git a/flask-dev/flask-web-dev/README.md b/flask-dev/flask-web-dev/README.md new file mode 100644 index 0000000..0c702fb --- /dev/null +++ b/flask-dev/flask-web-dev/README.md @@ -0,0 +1,13 @@ +- The dynamic components in routes are strings by default but can also be defined with a type. For example, route ```/user/ ``` would match only URLs that have an integer +in the id dynamic segment. + +- Flask supports types int, float, and path for routes. + +- The ```__name__ == '__main__' ``` Python idiom is used here to ensure that the development web server is started only when the script is executed directly. When the script is +imported by another script, it is assumed that the parent script will launch a different server, so the app.run() call is skipped. + +- During development, it is convenient to enable debug mode, which among other things activates the debugger and the re loader. This is done by passing the argument debug set to ```True```. + +- Request Hooks - Flask gives you the option to register common functions to be invoked before or after a request is dispatched to a view function + +- [Flask-Scripts](https://flask-script.readthedocs.io/en/latest/) : The Flask-Script extension provides support for writing external scripts in Flask. This includes running a development server, a customised Python shell, scripts to set up your database, cronjobs, and other command-line tasks that belong outside the web application itself. diff --git a/flask-dev/flask-web-dev/chap02/basic-web.py b/flask-dev/flask-web-dev/chap02/basic-web.py new file mode 100644 index 0000000..7ba2b2a --- /dev/null +++ b/flask-dev/flask-web-dev/chap02/basic-web.py @@ -0,0 +1,30 @@ +from flask import Flask +from flask import redirect +from flask import request + +app = Flask(__name__) + + +@app.route('/') +def home(): + browser_name = request.headers.get('User-agent') + return '

Hello Sohi %s

' % browser_name + + +@app.route('/user/') +def user(name): + return '

Hello, %s!

' % name + + +@app.route('/user/profile') +def profile(): + return '

Invalid URL

', 400 + + +@app.route('/search') +def search(): + return redirect('http://www.google.com') + + +if __name__ == '__main__': + app.run(debug=True) From 365b560085a5ab1730fad424d5bcb6104a1188b8 Mon Sep 17 00:00:00 2001 From: sohanvir Date: Tue, 29 Aug 2017 21:14:21 +0530 Subject: [PATCH 2/2] Added git ignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file