User:Kim/Stations, Skills, Resources/Flask: Difference between revisions
(→txt) |
|||
| (14 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Resources== | |||
[[User:Sevgi/Grad Notes/Flask]] | ===prototyping with manetta=== | ||
*'''[[Flask]]''' | |||
pad with collective notes: https://pad.xpub.nl/p/FLASK-OMG-itshappening | *[[User:Sevgi/Grad Notes/Flask]] | ||
*https://git.xpub.nl/tommi/flask-intro | |||
*pad with collective notes: https://pad.xpub.nl/p/FLASK-OMG-itshappening | |||
== Examples == | == Examples == | ||
=== txt === | === txt === | ||
''objective: website with textarea saves input to txt file and publishes entries on the page'' | ''objective: website with textarea saves input to txt file and publishes entries on the page''<br>following this example: [[Flask#Observations]] running on sergio: https://hub.xpub.nl/sergio/SI30/fieldwork-tools/example/<br> | ||
<code>observations-database.py</code> | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
from flask import Flask, render_template, request | from flask import Flask, render_template, request | ||
| Line 33: | Line 37: | ||
# output to database.txt: | # output to database.txt: | ||
return render_template("observations.html", database=database) | return render_template("observations.html", database=database) | ||
</syntaxhighlight> | |||
the template: <code>templates/observations.html</code> | |||
<syntaxhighlight lang="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> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== JSON === | === JSON === | ||
''objective: submit multiple inputs, store and retrieve them from json'' | ''objective: submit multiple inputs, store and retrieve them from json''<br> | ||
the json python module enables us to use json specific methods such as <code>dump</code><br> | |||
* https://www.w3schools.com/python/python_json.asp | |||
* https://www.geeksforgeeks.org/python/reading-and-writing-json-to-a-file-in-python/ | |||
<code>database.json</code><br> | |||
this file should be created. it does not require content yet but needs <code>[ ]</code> for the json objects to be inserted in between. <br> | |||
<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"> | |||
<style> | |||
body{ | |||
text-align: center; | |||
} | |||
div#archive{ | |||
padding: 1em; | |||
} | |||
div.container { | |||
border: 1px dashed red; | |||
border-radius: 20px; | |||
margin: 1vh 0; | |||
} | |||
div.container:has(.sunny) { | |||
background: yellow; | |||
} | |||
div.container:has(.rainy) { | |||
background: lightsteelblue; | |||
} | |||
div.container:has(.sunny):has(.rainy) { | |||
background: linear-gradient(yellow, lightsteelblue); | |||
} | |||
form { | |||
margin: 1em; | |||
} | |||
form textarea{ | |||
width: 100%; | |||
height: 200px; | |||
} | |||
form input{ | |||
margin-top: 1em; | |||
font-size: 12pt; | |||
} | |||
</style> | |||
<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="container"> | |||
<div class="place">{{ observation.place }}</div> | |||
<div class="observation">{{ observation.observation }}</div> | |||
{% if observation.sunny %} | |||
<div class="sunny">☀</div> | |||
{% endif %} | |||
{% if observation.rainy %} | |||
<div class="rainy">☁</div> | |||
{% endif %} | |||
</div> | |||
{% endfor %} | |||
{% endif %} | |||
</div> | |||
<form method="POST"> | |||
<textarea name="observation"></textarea> | |||
<input type="text" name="place"></input> | |||
<label for="place">place</label> | |||
<input type="checkbox" name="sunny" id=""> | |||
<label for="sunny">sunny</label> | |||
<input type="checkbox" name="rainy" id=""> | |||
<label for="rainy">rainy</label> | |||
<br> | |||
<input type="submit" value="Save!"> | |||
</form> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Flask x GitPub== | |||
* similar to what we did with json, i could work with yaml (https://python.land/data-processing/python-yaml) which i am already using in [[User:Kim/Prototyping 2nd year/GitPub Cite|GitPub Cite]] | |||
* server ?? !! | |||
Latest revision as of 14:48, 13 April 2026
Resources
prototyping with manetta
- Flask
- User:Sevgi/Grad Notes/Flask
- https://git.xpub.nl/tommi/flask-intro
- 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
following this example: Flask#Observations running on sergio: https://hub.xpub.nl/sergio/SI30/fieldwork-tools/example/
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
the json python module enables us to use json specific methods such as dump
- https://www.w3schools.com/python/python_json.asp
- https://www.geeksforgeeks.org/python/reading-and-writing-json-to-a-file-in-python/
database.json
this file should be created. it does not require content yet but needs [ ] for the json objects to be inserted in between.
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
<style>
body{
text-align: center;
}
div#archive{
padding: 1em;
}
div.container {
border: 1px dashed red;
border-radius: 20px;
margin: 1vh 0;
}
div.container:has(.sunny) {
background: yellow;
}
div.container:has(.rainy) {
background: lightsteelblue;
}
div.container:has(.sunny):has(.rainy) {
background: linear-gradient(yellow, lightsteelblue);
}
form {
margin: 1em;
}
form textarea{
width: 100%;
height: 200px;
}
form input{
margin-top: 1em;
font-size: 12pt;
}
</style>
<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="container">
<div class="place">{{ observation.place }}</div>
<div class="observation">{{ observation.observation }}</div>
{% if observation.sunny %}
<div class="sunny">☀</div>
{% endif %}
{% if observation.rainy %}
<div class="rainy">☁</div>
{% endif %}
</div>
{% endfor %}
{% endif %}
</div>
<form method="POST">
<textarea name="observation"></textarea>
<input type="text" name="place"></input>
<label for="place">place</label>
<input type="checkbox" name="sunny" id="">
<label for="sunny">sunny</label>
<input type="checkbox" name="rainy" id="">
<label for="rainy">rainy</label>
<br>
<input type="submit" value="Save!">
</form>
Flask x GitPub
- similar to what we did with json, i could work with yaml (https://python.land/data-processing/python-yaml) which i am already using in GitPub Cite
- server ?? !!