Skip to content

Commit 80ffe84

Browse files
committed
Web now covers Flask instead of Bottle
1 parent 8e31bee commit 80ffe84

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,49 +2525,50 @@ except requests.exceptions.ConnectionError:
25252525

25262526
Web
25272527
---
2528-
**Bottle is a micro web framework/server. If you just want to open a html file in a web browser use `'webbrowser.open(<path>)'` instead.**
2528+
**Flask is a micro web framework/server. If you just want to open a html file in a web browser use `'webbrowser.open(<path>)'` instead.**
25292529
```python
2530-
# $ pip3 install bottle
2531-
from bottle import run, route, static_file, template, post, request, response
2532-
import json
2530+
# $ pip3 install flask
2531+
from flask import Flask, send_from_directory, render_template_string, request
25332532
```
25342533

2535-
### Run
25362534
```python
2537-
run(host='localhost', port=8080) # Runs locally.
2538-
run(host='0.0.0.0', port=80) # Runs globally.
2535+
app = Flask(__name__)
2536+
app.run()
25392537
```
2538+
* **Starts the app on `'http://localhost:5000'`.**
2539+
* **You will need a WSGI server like [waitress](https://flask.palletsprojects.com/en/latest/deploying/waitress/) and a HTTP server such as [nginx](https://flask.palletsprojects.com/en/latest/deploying/nginx/) to run globally.**
2540+
25402541

25412542
### Static Request
25422543
```python
2543-
@route('/img/<filename>')
2544-
def send_file(filename):
2545-
return static_file(filename, root='img_dir/')
2544+
@app.route('/img/<path:filename>')
2545+
def serve_file(filename):
2546+
return send_from_directory('dirname/', filename)
25462547
```
25472548

25482549
### Dynamic Request
25492550
```python
2550-
@route('/<sport>')
2551-
def send_html(sport):
2552-
return template('<h1>{{title}}</h1>', title=sport)
2551+
@app.route('/<sport>')
2552+
def serve_html(sport):
2553+
return render_template_string('<h1>{{title}}</h1>', title=sport)
25532554
```
2555+
* **`'render_template()'` accepts filename of a template stored in 'templates' directory.**
25542556

25552557
### REST Request
25562558
```python
2557-
@post('/<sport>/odds')
2558-
def send_json(sport):
2559-
team = request.forms.get('team')
2560-
response.headers['Content-Type'] = 'application/json'
2561-
response.headers['Cache-Control'] = 'no-cache'
2562-
return json.dumps({'team': team, 'odds': [2.09, 3.74, 3.68]})
2559+
@app.route('/<sport>/odds', methods=['POST'])
2560+
def serve_json(sport):
2561+
team = request.form['team']
2562+
return {'team': team, 'odds': [2.09, 3.74, 3.68]}
25632563
```
2564+
* **To get a parameter from the query string (part after the ?) use `'request.args.get(<str>)'`.**
25642565

25652566
#### Test:
25662567
```python
25672568
# $ pip3 install requests
25682569
>>> import threading, requests
2569-
>>> threading.Thread(target=run, daemon=True).start()
2570-
>>> url = 'http://localhost:8080/football/odds'
2570+
>>> threading.Thread(target=app.run, daemon=True).start()
2571+
>>> url = 'http://localhost:5000/football/odds'
25712572
>>> request_data = {'team': 'arsenal f.c.'}
25722573
>>> response = requests.post(url, data=request_data)
25732574
>>> response.json()
@@ -2577,7 +2578,7 @@ def send_json(sport):
25772578

25782579
Profiling
25792580
---------
2580-
### Stopwatch
2581+
25812582
```python
25822583
from time import perf_counter
25832584
start_time = perf_counter()

index.html

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,50 +2071,55 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20712071
</code></pre></div></div>
20722072

20732073

2074-
<div><h2 id="web"><a href="#web" name="web">#</a>Web</h2><p><strong>Bottle is a micro web framework/server. If you just want to open a html file in a web browser use <code class="python hljs"><span class="hljs-string">'webbrowser.open(&lt;path&gt;)'</span></code> instead.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install bottle</span>
2075-
<span class="hljs-keyword">from</span> bottle <span class="hljs-keyword">import</span> run, route, static_file, template, post, request, response
2076-
<span class="hljs-keyword">import</span> json
2074+
<div><h2 id="web"><a href="#web" name="web">#</a>Web</h2><p><strong>Flask is a micro web framework/server. If you just want to open a html file in a web browser use <code class="python hljs"><span class="hljs-string">'webbrowser.open(&lt;path&gt;)'</span></code> instead.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install flask</span>
2075+
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, send_from_directory, render_template_string, request
20772076
</code></pre></div>
20782077

20792078

2080-
<div><h3 id="run">Run</h3><pre><code class="python language-python hljs">run(host=<span class="hljs-string">'localhost'</span>, port=<span class="hljs-number">8080</span>) <span class="hljs-comment"># Runs locally.</span>
2081-
run(host=<span class="hljs-string">'0.0.0.0'</span>, port=<span class="hljs-number">80</span>) <span class="hljs-comment"># Runs globally.</span>
2082-
</code></pre></div>
2083-
2084-
<div><h3 id="staticrequest">Static Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@route('/img/&lt;filename&gt;')</span>
2085-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_file</span><span class="hljs-params">(filename)</span>:</span>
2086-
<span class="hljs-keyword">return</span> static_file(filename, root=<span class="hljs-string">'img_dir/'</span>)
2079+
<pre><code class="python language-python hljs">app = Flask(__name__)
2080+
app.run()
2081+
</code></pre>
2082+
<ul>
2083+
<li><strong>Starts the app on <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>.</strong></li>
2084+
<li><strong>You will need a WSGI server like <a href="https://flask.palletsprojects.com/en/latest/deploying/waitress/">waitress</a> and a HTTP server such as <a href="https://flask.palletsprojects.com/en/latest/deploying/nginx/">nginx</a> to run globally.</strong></li>
2085+
</ul>
2086+
<div><h3 id="staticrequest">Static Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.route('/img/&lt;path:filename&gt;')</span>
2087+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_file</span><span class="hljs-params">(filename)</span>:</span>
2088+
<span class="hljs-keyword">return</span> send_from_directory(<span class="hljs-string">'dirname/'</span>, filename)
20872089
</code></pre></div>
20882090

2089-
<div><h3 id="dynamicrequest">Dynamic Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@route('/&lt;sport&gt;')</span>
2090-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_html</span><span class="hljs-params">(sport)</span>:</span>
2091-
<span class="hljs-keyword">return</span> template(<span class="hljs-string">'&lt;h1&gt;{{title}}&lt;/h1&gt;'</span>, title=sport)
2091+
<div><h3 id="dynamicrequest">Dynamic Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.route('/&lt;sport&gt;')</span>
2092+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_html</span><span class="hljs-params">(sport)</span>:</span>
2093+
<span class="hljs-keyword">return</span> render_template_string(<span class="hljs-string">'&lt;h1&gt;{{title}}&lt;/h1&gt;'</span>, title=sport)
20922094
</code></pre></div>
20932095

2094-
<div><h3 id="restrequest">REST Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@post('/&lt;sport&gt;/odds')</span>
2095-
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_json</span><span class="hljs-params">(sport)</span>:</span>
2096-
team = request.forms.get(<span class="hljs-string">'team'</span>)
2097-
response.headers[<span class="hljs-string">'Content-Type'</span>] = <span class="hljs-string">'application/json'</span>
2098-
response.headers[<span class="hljs-string">'Cache-Control'</span>] = <span class="hljs-string">'no-cache'</span>
2099-
<span class="hljs-keyword">return</span> json.dumps({<span class="hljs-string">'team'</span>: team, <span class="hljs-string">'odds'</span>: [<span class="hljs-number">2.09</span>, <span class="hljs-number">3.74</span>, <span class="hljs-number">3.68</span>]})
2096+
<ul>
2097+
<li><strong><code class="python hljs"><span class="hljs-string">'render_template()'</span></code> accepts filename of a template stored in 'templates' directory.</strong></li>
2098+
</ul>
2099+
<div><h3 id="restrequest">REST Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.route('/&lt;sport&gt;/odds', methods=['POST'])</span>
2100+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_json</span><span class="hljs-params">(sport)</span>:</span>
2101+
team = request.form[<span class="hljs-string">'team'</span>]
2102+
<span class="hljs-keyword">return</span> {<span class="hljs-string">'team'</span>: team, <span class="hljs-string">'odds'</span>: [<span class="hljs-number">2.09</span>, <span class="hljs-number">3.74</span>, <span class="hljs-number">3.68</span>]}
21002103
</code></pre></div>
21012104

2105+
<ul>
2106+
<li><strong>To get a parameter from the query string (part after the ?) use <code class="python hljs"><span class="hljs-string">'request.args.get(&lt;str&gt;)'</span></code>.</strong></li>
2107+
</ul>
21022108
<div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
21032109
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> threading, requests
2104-
<span class="hljs-meta">&gt;&gt;&gt; </span>threading.Thread(target=run, daemon=<span class="hljs-keyword">True</span>).start()
2105-
<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">'http://localhost:8080/football/odds'</span>
2110+
<span class="hljs-meta">&gt;&gt;&gt; </span>threading.Thread(target=app.run, daemon=<span class="hljs-keyword">True</span>).start()
2111+
<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">'http://localhost:5000/football/odds'</span>
21062112
<span class="hljs-meta">&gt;&gt;&gt; </span>request_data = {<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>}
21072113
<span class="hljs-meta">&gt;&gt;&gt; </span>response = requests.post(url, data=request_data)
21082114
<span class="hljs-meta">&gt;&gt;&gt; </span>response.json()
21092115
{<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>, <span class="hljs-string">'odds'</span>: [<span class="hljs-number">2.09</span>, <span class="hljs-number">3.74</span>, <span class="hljs-number">3.68</span>]}
21102116
</code></pre></div>
21112117

2112-
<div><h2 id="profiling"><a href="#profiling" name="profiling">#</a>Profiling</h2><div><h3 id="stopwatch">Stopwatch</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> perf_counter
2118+
<div><h2 id="profiling"><a href="#profiling" name="profiling">#</a>Profiling</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> perf_counter
21132119
start_time = perf_counter()
21142120
...
21152121
duration_in_seconds = perf_counter() - start_time
2116-
</code></pre></div></div>
2117-
2122+
</code></pre></div>
21182123

21192124
<div><h3 id="timingasnippet">Timing a Snippet</h3><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> timeit <span class="hljs-keyword">import</span> timeit
21202125
<span class="hljs-meta">&gt;&gt;&gt; </span>timeit(<span class="hljs-string">"''.join(str(i) for i in range(100))"</span>,

0 commit comments

Comments
 (0)