User:Kim/Stations, Skills, Resources/Flask: Difference between revisions
(→txt) |
(→JSON) |
||
| Line 61: | Line 61: | ||
=== JSON === | === JSON === | ||
''objective: submit multiple inputs, store and retrieve them from json'' | ''objective: submit multiple inputs, store and retrieve them from json'' | ||
<code>json-test.py</code> | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
import json | |||
from flask import Flask, render_template, request | |||
app = Flask(__name__) | |||
@app.route("/", methods=["GET", "POST"]) | |||
def main(): | |||
with open('database.json', 'r') as f: | |||
observations = json.load(f) | |||
if request.method == "POST": | |||
observation = request.form | |||
print(observation) | |||
observations.append(observation) | |||
with open('database.json', 'w+') as f: | |||
f.write(json.dumps(observations, indent=2)) | |||
return render_template("json.html", observations=observations) | |||
</syntaxhighlight> | |||
<code>templates/json.html</code> | |||
<syntaxhighlight lang="html"> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 14:14, 13 April 2026
pad with collective notes: https://pad.xpub.nl/p/FLASK-OMG-itshappening
Examples
txt
objective: website with textarea saves input to txt file and publishes entries on the page
observations-database.py
from flask import Flask, render_template, request
app = Flask(__name__)
# define route
# when the route is being accessed, run function main with get and post request
@app.route("/", methods=["GET", "POST"])
# function main gets form input and renders a template (render and request are methods imported from flask)
def main():
# is request is post
if request.method == "POST":
# request the form (in the html template)
observation = request.form["observation"]
# append the form input to observations
observations.append(observation)
# append observation to txt file
# make txt file
with open("database.txt", "a") as database:
# we are only wiriting on post requests
database.write("\n"+ observation)
# reading is for opening the app, when we dont write first (code is not entirely running linearly)
database = open("database.txt", "r").readlines()
# output to database.txt:
return render_template("observations.html", database=database)
the template: templates/observations.html
<h1>My field observations</h1>
<div id="archive">
<!-- if there is an object in the observations array -->
{% if observations %}
<!-- for each observation in the array -->
{% for observation in observations %}
<!-- display observation in a div -->
<div class="observation">{{ observation }}</div>
{% endfor %}
{% endif %}
</div>
<form method="POST">
<textarea name="observation"></textarea>
<br>
<input type="submit" value="Save!">
</form>
JSON
objective: submit multiple inputs, store and retrieve them from json
json-test.py
import json
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def main():
with open('database.json', 'r') as f:
observations = json.load(f)
if request.method == "POST":
observation = request.form
print(observation)
observations.append(observation)
with open('database.json', 'w+') as f:
f.write(json.dumps(observations, indent=2))
return render_template("json.html", observations=observations)
templates/json.html