Browse Source

Web

pull/109/merge
Jure Šorn 1 year ago
parent
commit
e435a5a949
3 changed files with 21 additions and 20 deletions
  1. 17
      README.md
  2. 22
      index.html
  3. 2
      pdf/index_for_pdf.html

17
README.md

@ -2539,11 +2539,11 @@ from flask import Flask, send_from_directory, render_template_string, request
```python ```python
app = Flask(__name__) app = Flask(__name__)
app.run()
app.run(host=None, debug=None)
``` ```
* **Starts the app on `'http://localhost:5000'`.**
* **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.**
* **Starts the app at `'http://localhost:5000'`. Use `'host="0.0.0.0"'` to run externally.**
* **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.**
* **Debug mode restarts the app whenever script changes and displays errors in the browser.**
### Static Request ### Static Request
```python ```python
@ -2558,18 +2558,19 @@ def serve_file(filename):
def serve_html(sport): def serve_html(sport):
return render_template_string('<h1>{{title}}</h1>', title=sport) return render_template_string('<h1>{{title}}</h1>', title=sport)
``` ```
* **`'render_template()'` accepts filename of a template stored in 'templates' directory.**
* **To return an error code use `'abort(<int>)'` and to redirect use `'redirect(<url>)'`.**
* **`'request.args[<str>]'` returns parameter from the query string (URL part after the ?).**
* **Use `'session[key] = value'` to store session data like username, etc.**
### REST Request ### REST Request
```python ```python
@app.route('/<sport>/odds', methods=['POST'])
@app.post('/<sport>/odds')
def serve_json(sport): def serve_json(sport):
team = request.form['team'] team = request.form['team']
return {'team': team, 'odds': [2.09, 3.74, 3.68]} return {'team': team, 'odds': [2.09, 3.74, 3.68]}
``` ```
* **To get a parameter from the query string (part after the ?) use `'request.args.get(<str>)'`.**
#### Test:
#### Starts the app in its own thread and queries it with a post request:
```python ```python
# $ pip3 install requests # $ pip3 install requests
>>> import threading, requests >>> import threading, requests

22
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>April 5, 2023</aside>
<aside>April 8, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -2079,11 +2079,12 @@ WIKI_URL = <span class="hljs-string">'https://en.wikipedia.org/wiki/Python_(prog
<pre><code class="python language-python hljs">app = Flask(__name__) <pre><code class="python language-python hljs">app = Flask(__name__)
app.run()
app.run(host=<span class="hljs-keyword">None</span>, debug=<span class="hljs-keyword">None</span>)
</code></pre> </code></pre>
<ul> <ul>
<li><strong>Starts the app on <code class="python hljs"><span class="hljs-string">'http://localhost:5000'</span></code>.</strong></li>
<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>
<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>
<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>
<li><strong>Debug mode restarts the app whenever script changes and displays errors in the browser.</strong></li>
</ul> </ul>
<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> <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>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_file</span><span class="hljs-params">(filename)</span>:</span> <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 @@ app.run()
</code></pre></div> </code></pre></div>
<ul> <ul>
<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>
<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>
<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>
<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>
</ul> </ul>
<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>
<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>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_json</span><span class="hljs-params">(sport)</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serve_json</span><span class="hljs-params">(sport)</span>:</span>
team = request.form[<span class="hljs-string">'team'</span>] team = request.form[<span class="hljs-string">'team'</span>]
<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>]} <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>]}
</code></pre></div> </code></pre></div>
<ul>
<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>
</ul>
<div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
<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>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> threading, requests <span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> threading, requests
<span class="hljs-meta">&gt;&gt;&gt; </span>threading.Thread(target=app.run, daemon=<span class="hljs-keyword">True</span>).start() <span class="hljs-meta">&gt;&gt;&gt; </span>threading.Thread(target=app.run, daemon=<span class="hljs-keyword">True</span>).start()
<span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">'http://localhost:5000/football/odds'</span> <span class="hljs-meta">&gt;&gt;&gt; </span>url = <span class="hljs-string">'http://localhost:5000/football/odds'</span>
@ -2934,7 +2934,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer> <footer>
<aside>April 5, 2023</aside>
<aside>April 8, 2023</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

2
pdf/index_for_pdf.html

@ -115,7 +115,7 @@
<strong>recursion, <a href="#lrucache">13</a></strong><br> <strong>recursion, <a href="#lrucache">13</a></strong><br>
<strong>reduce function, <a href="#mapfilterreduce">11</a>, <a href="#operator">31</a></strong><br> <strong>reduce function, <a href="#mapfilterreduce">11</a>, <a href="#operator">31</a></strong><br>
<strong>regular expressions, <a href="#regex">5</a>-<a href="#specialsequences">6</a></strong><br> <strong>regular expressions, <a href="#regex">5</a>-<a href="#specialsequences">6</a></strong><br>
<strong>requests library, <a href="#scrapespythonsurlversionnumberandlogofromitswikipediapage">35</a>, <a href="#test">36</a></strong> </p>
<strong>requests library, <a href="#scrapespythonsurlversionnumberandlogofromitswikipediapage">35</a>, <a href="#startstheappinitsownthreadandqueriesitwithapostrequest">36</a></strong> </p>
<h3 id="s">S</h3> <h3 id="s">S</h3>
<p><strong>scope, <a href="#insidefunctiondefinition">10</a>, <a href="#nonlocal">12</a>, <a href="#complexexample">20</a></strong><br> <p><strong>scope, <a href="#insidefunctiondefinition">10</a>, <a href="#nonlocal">12</a>, <a href="#complexexample">20</a></strong><br>
<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> <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>

Loading…
Cancel
Save