From e435a5a94973fbcc6b05b3db1f38105722ee36c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 8 Apr 2023 07:09:32 +0200 Subject: [PATCH] Web --- README.md | 17 +++++++++-------- index.html | 22 +++++++++++----------- pdf/index_for_pdf.html | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1308bee..70ce44e 100644 --- a/README.md +++ b/README.md @@ -2539,11 +2539,11 @@ from flask import Flask, send_from_directory, render_template_string, request ```python 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 ```python @@ -2558,18 +2558,19 @@ def serve_file(filename): def serve_html(sport): return render_template_string('

{{title}}

', title=sport) ``` -* **`'render_template()'` accepts filename of a template stored in 'templates' directory.** +* **To return an error code use `'abort()'` and to redirect use `'redirect()'`.** +* **`'request.args[]'` returns parameter from the query string (URL part after the ?).** +* **Use `'session[key] = value'` to store session data like username, etc.** ### REST Request ```python -@app.route('//odds', methods=['POST']) +@app.post('//odds') def serve_json(sport): team = request.form['team'] 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()'`.** -#### Test: +#### Starts the app in its own thread and queries it with a post request: ```python # $ pip3 install requests >>> import threading, requests diff --git a/index.html b/index.html index ae5be46..9d134f7 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -2079,11 +2079,12 @@ WIKI_URL = 'https://en.wikipedia.org/wiki/Python_(prog
app = Flask(__name__)
-app.run()
+app.run(host=None, debug=None)
 
    -
  • Starts the app on 'http://localhost:5000'.
  • -
  • A WSGI server like Waitress and a HTTP server such as 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 and a HTTP server such as Nginx for better security.
  • +
  • Debug mode restarts the app whenever script changes and displays errors in the browser.

Static Request

@app.route('/img/<path:filename>')
 def serve_file(filename):
@@ -2096,18 +2097,17 @@ app.run()
 
    -
  • '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

@app.route('/<sport>/odds', methods=['POST'])
+

REST Request

@app.post('/<sport>/odds')
 def serve_json(sport):
     team = request.form['team']
     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:

# $ pip3 install requests
+

Starts the app in its own thread and queries it with a post request:

# $ pip3 install requests
 >>> import threading, requests
 >>> threading.Thread(target=app.run, daemon=True).start()
 >>> url = 'http://localhost:5000/football/odds'
@@ -2934,7 +2934,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
  
 
   
 
diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html
index 72dd72b..9b80128 100644
--- a/pdf/index_for_pdf.html
+++ b/pdf/index_for_pdf.html
@@ -115,7 +115,7 @@
 recursion, 13
reduce function, 11, 31
regular expressions, 5-6
-requests library, 35, 36

+requests library, 35, 36

S

scope, 10, 12, 20
scraping, 35, 43, 46, 47-48