Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e435a5a

Browse files
committed
Web
1 parent c58b381 commit e435a5a

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,11 +2539,11 @@ from flask import Flask, send_from_directory, render_template_string, request
25392539

25402540
```python
25412541
app = Flask(__name__)
2542-
app.run()
2542+
app.run(host=None, debug=None)
25432543
```
2544-
* **Starts the app on `'http://localhost:5000'`.**
2545-
* **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/) are needed to run globally.**
2546-
2544+
* **Starts the app at `'http://localhost:5000'`. Use `'host="0.0.0.0"'` to run externally.**
2545+
* **Install 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/) for better security.**
2546+
* **Debug mode restarts the app whenever script changes and displays errors in the browser.**
25472547

25482548
### Static Request
25492549
```python
@@ -2558,18 +2558,19 @@ def serve_file(filename):
25582558
def serve_html(sport):
25592559
return render_template_string('<h1>{{title}}</h1>', title=sport)
25602560
```
2561-
* **`'render_template()'` accepts filename of a template stored in 'templates' directory.**
2561+
* **To return an error code use `'abort(<int>)'` and to redirect use `'redirect(<url>)'`.**
2562+
* **`'request.args[<str>]'` returns parameter from the query string (URL part after the ?).**
2563+
* **Use `'session[key] = value'` to store session data like username, etc.**
25622564

25632565
### REST Request
25642566
```python
2565-
@app.route('/<sport>/odds', methods=['POST'])
2567+
@app.post('/<sport>/odds')
25662568
def serve_json(sport):
25672569
team = request.form['team']
25682570
return {'team': team, 'odds': [2.09, 3.74, 3.68]}
25692571
```
2570-
* **To get a parameter from the query string (part after the ?) use `'request.args.get(<str>)'`.**
25712572

2572-
#### Test:
2573+
#### Starts the app in its own thread and queries it with a post request:
25732574
```python
25742575
# $ pip3 install requests
25752576
>>> import threading, requests

index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>April 5, 2023</aside>
57+
<aside>April 8, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -2079,11 +2079,12 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20792079

20802080

20812081
<pre><code class="python language-python hljs">app = Flask(__name__)
2082-
app.run()
2082+
app.run(host=<span class="hljs-keyword">None</span>, debug=<span class="hljs-keyword">None</span>)
20832083
</code></pre>
20842084
<ul>
2085-
<li><strong>Starts the app on <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>.</strong></li>
2086-
<li><strong>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> are needed to run globally.</strong></li>
2085+
<li><strong>Starts the app at <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>. Use <code class="python hljs"><span class="hljs-string">'host="0.0.0.0"'</span></code> to run externally.</strong></li>
2086+
<li><strong>Install 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> for better security.</strong></li>
2087+
<li><strong>Debug mode restarts the app whenever script changes and displays errors in the browser.</strong></li>
20872088
</ul>
20882089
<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>
20892090
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_file</span><span class="hljs-params">(filename)</span>:</span>
@@ -2096,18 +2097,17 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
20962097
</code></pre></div>
20972098

20982099
<ul>
2099-
<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>
2100+
<li><strong>To return an error code use <code class="python hljs"><span class="hljs-string">'abort(&lt;int&gt;)'</span></code> and to redirect use <code class="python hljs"><span class="hljs-string">'redirect(&lt;url&gt;)'</span></code>.</strong></li>
2101+
<li><strong><code class="python hljs"><span class="hljs-string">'request.args[&lt;str&gt;]'</span></code> returns parameter from the query string (URL part after the ?).</strong></li>
2102+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'session[key] = value'</span></code> to store session data like username, etc.</strong></li>
21002103
</ul>
2101-
<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>
2104+
<div><h3 id="restrequest">REST Request</h3><pre><code class="python language-python hljs"><span class="hljs-meta">@app.post('/&lt;sport&gt;/odds')</span>
21022105
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_json</span><span class="hljs-params">(sport)</span>:</span>
21032106
team = request.form[<span class="hljs-string">'team'</span>]
21042107
<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>]}
21052108
</code></pre></div>
21062109

2107-
<ul>
2108-
<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>
2109-
</ul>
2110-
<div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
2110+
<div><h4 id="startstheappinitsownthreadandqueriesitwithapostrequest">Starts the app in its own thread and queries it with a post request:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
21112111
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> threading, requests
21122112
<span class="hljs-meta">&gt;&gt;&gt; </span>threading.Thread(target=app.run, daemon=<span class="hljs-keyword">True</span>).start()
21132113
<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">'http://localhost:5000/football/odds'</span>
@@ -2934,7 +2934,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29342934

29352935

29362936
<footer>
2937-
<aside>April 5, 2023</aside>
2937+
<aside>April 8, 2023</aside>
29382938
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29392939
</footer>
29402940

pdf/index_for_pdf.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ <h3 id="r">R</h3>
115115
<strong>recursion, <a href="#lrucache">13</a></strong><br>
116116
<strong>reduce function, <a href="#mapfilterreduce">11</a>, <a href="#operator">31</a></strong><br>
117117
<strong>regular expressions, <a href="#regex">5</a>-<a href="#specialsequences">6</a></strong><br>
118-
<strong>requests library, <a href="#scrapespythonsurlversionnumberandlogofromitswikipediapage">35</a>, <a href="#test">36</a></strong> </p>
118+
<strong>requests library, <a href="#scrapespythonsurlversionnumberandlogofromitswikipediapage">35</a>, <a href="#startstheappinitsownthreadandqueriesitwithapostrequest">36</a></strong> </p>
119119
<h3 id="s">S</h3>
120120
<p><strong>scope, <a href="#insidefunctiondefinition">10</a>, <a href="#nonlocal">12</a>, <a href="#complexexample">20</a></strong><br>
121121
<strong>scraping, <a href="#scraping">35</a>, <a href="#basicmariobrothersexample">43</a>, <a href="#dataframeencodedecodeplot">46</a>, <a href="#displaysalinechartoftotalcoronavirusdeathspermilliongroupedbycontinent">47</a>-<a href="#displaysamultiaxislinechartoftotalcoronaviruscasesandchangesinpricesofbitcoindowjonesandgold">48</a></strong><br>

0 commit comments

Comments
 (0)