User:Kim/Stations, Skills, Resources/Flask: Difference between revisions

From XPUB & Lens-Based wiki
Line 9: Line 9:
=== 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''
<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 35:
     # 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:
<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>



Revision as of 14:12, 13 April 2026

Flask

User:Sevgi/Grad Notes/Flask

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:

<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