Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
talit:flask_webserver [2023-10-02 10:13] – [HTTP-Requests an Server] sca | talit:flask_webserver [2023-12-18 15:25] (aktuell) – sca | ||
---|---|---|---|
Zeile 2: | Zeile 2: | ||
===== Grundgerüst ===== | ===== Grundgerüst ===== | ||
+ | |||
+ | ==== Übersicht ==== | ||
+ | |||
+ | Erstelle folgende Files | ||
* Flask-App `app.py` in Hauptordner | * Flask-App `app.py` in Hauptordner | ||
* HTML-Seite `index.html` in Unterordner `templates` | * HTML-Seite `index.html` in Unterordner `templates` | ||
* CSS- & JavaScript-Files `style.css` & `script.js` in Unterordner `static` | * CSS- & JavaScript-Files `style.css` & `script.js` in Unterordner `static` | ||
+ | |||
+ | ==== Setup & Ausführen ==== | ||
+ | |||
+ | * Installiere Flask with pip | ||
+ | * Führe `app.py` aus, dann klicke auf den Link in der Konsole und du solltest auf die Website gelangen. | ||
+ | |||
+ | ==== Files im Detail ==== | ||
<code python app.py> | <code python app.py> | ||
- | from flask import Flask, render_template # import the Flask object from the flask package | + | from flask import Flask, render_template, request, jsonify |
+ | import datetime | ||
+ | import random | ||
app = Flask(__name__) # create Flask application instance with name ' | app = Flask(__name__) # create Flask application instance with name ' | ||
- | app.debug = True | + | #app.debug = True |
# HTTP REQUESTS AND RESPONSES | # HTTP REQUESTS AND RESPONSES | ||
+ | # return root resource index.html for first get request "GET /" | ||
+ | @app.route('/' | ||
+ | def index(): | ||
+ | return render_template(' | ||
+ | |||
+ | @app.route('/ | ||
+ | def do_something(): | ||
+ | data = request.json[' | ||
+ | # do something with data | ||
+ | # e.g. create dictionary with response data | ||
+ | data_response = {" | ||
+ | return data_response | ||
+ | |||
+ | if __name__ == ' | ||
+ | app.run() | ||
+ | |||
""" | """ | ||
typical shape | typical shape | ||
Zeile 19: | Zeile 48: | ||
def some_fcn(): # ... regular Python function into a Flask view function which ... | def some_fcn(): # ... regular Python function into a Flask view function which ... | ||
... | ... | ||
- | return ... # ... converts the function' | + | return ... # ... converts the function' |
""" | """ | ||
- | |||
- | @app.route('/' | ||
- | def index(): | ||
- | return render_template(' | ||
- | |||
- | if __name__ == ' | ||
- | app.run() | ||
- | |||
</ | </ | ||
Zeile 44: | Zeile 65: | ||
< | < | ||
+ | ... | ||
+ | <!-- can access dictionary with data that is passed as argument (see app.py above) --> | ||
+ | <p>My data: {{ data.name_of_key }}</ | ||
... | ... | ||
</ | </ | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | Der JavaScript Code hat folgende Struktur. Im nächsten Kapitel werden die wichtigen Elemente im Detail erklärt. | ||
+ | |||
+ | |||
+ | <code javascript script.js> | ||
+ | // access html elements from withing JS | ||
+ | let htmlElement = document.getElementById(" | ||
+ | |||
+ | // function that interacts with flask server app | ||
+ | async function interactWithServer(word){ | ||
+ | // send post request to server | ||
+ | let response = await fetch('/ | ||
+ | method: ' | ||
+ | headers: { // tells server that request body contains json data | ||
+ | ' | ||
+ | }, | ||
+ | body: JSON.stringify({" | ||
+ | }); | ||
+ | |||
+ | // write response status (hopefully not 404!) into console | ||
+ | console.log(response.status); | ||
+ | |||
+ | // wait for server' | ||
+ | let json_data = await response.json(); | ||
+ | |||
+ | // now do something with data | ||
+ | htmlElement.innerHTML = json_data.nameOfKey; | ||
+ | } | ||
+ | |||
+ | // function that is executed on even | ||
+ | function somethingHasHappened(event){ | ||
+ | // call function that interacts with server | ||
+ | interactWithServer(inpPlainText.value); | ||
+ | } | ||
+ | |||
+ | // add event listeners (clicking button, typing something into input box, ...) | ||
+ | htmlElement.addEventListener(' | ||
</ | </ | ||
Zeile 87: | Zeile 149: | ||
' | ' | ||
}, | }, | ||
- | body: JSON.stringify({data: | + | body: JSON.stringify({data: |
}) | }) | ||
</ | </ | ||
- | Es gibt auch noch eine alternative, etwas kompaktere | + | Es gibt auch noch eine alternative Variante für das Senden eines HTTP-Requests, die obige Variante mit asynv & await ist aber zu bevorzugen. |
+ | |||
+ | ++++Alternativer Syntax für HTTP-Request| | ||
<code javascript> | <code javascript> | ||
Zeile 100: | Zeile 164: | ||
' | ' | ||
}, | }, | ||
- | body: JSON.stringify({data: | + | body: JSON.stringify({data: |
}) | }) | ||
.then(function(response) { | .then(function(response) { | ||
Zeile 114: | Zeile 178: | ||
</ | </ | ||
- | Sendet man einen HTTP-Request an seinen Flask-Server, | + | ++++ |
+ | |||
+ | Sendet man einen HTTP-Request an seinen | ||
<code python> | <code python> | ||
@app.route('/ | @app.route('/ | ||
def do_this(): | def do_this(): | ||
... | ... | ||
- | return {" | + | return {" |
</ | </ | ||
+ | |||