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 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)