Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
13 changes: 13 additions & 0 deletions flask-dev/flask-web-dev/README.md
Original file line number Diff line number Diff line change
@@ -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/<int:id> ``` 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.
30 changes: 30 additions & 0 deletions flask-dev/flask-web-dev/chap02/basic-web.py
Original file line number Diff line number Diff line change
@@ -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 '<h1>Hello Sohi %s</h1>' % browser_name


@app.route('/user/<name>')
def user(name):
return '<h1>Hello, %s!</h1>' % name


@app.route('/user/profile')
def profile():
return '<h2>Invalid URL </h2>', 400


@app.route('/search')
def search():
return redirect('http://www.google.com')


if __name__ == '__main__':
app.run(debug=True)