I want to make the /update to be protected with a password so that it can only be accessed if the user enters the right password. The table_values being shown on index.html. On update.html the same are shown but it is possible to edit them. After submitting it the dictionary is updated thus the values on the index.html too.
How can I implement /update to be password protected?
from flask import Flask, render_template, redirect, url_for, request
import json
import os
app = Flask(__name__)
# Set the path to the file where the values will be stored
VALUES_FILE_PATH = 'table_values.json'
# Initial values for the table
# Check if the values file exists
if os.path.exists(VALUES_FILE_PATH):
# Read the values from the file
with open(VALUES_FILE_PATH, 'r') as file:
table_values = json.load(file)
else:
# If the file doesn't exist, use the initial values
table_values = {
'eur_buy': "4.94",
'eur_sell': "4.97",
'usd_buy': "4.50",
'usd_sell': "4.60"}
@app.route('/')
def home():
return render_template('index.html', values=table_values)
@app.route('/update', methods=['GET', 'POST'])
def update():
if request.method == 'POST':
# Iterate over the keys in table_values and update their values from the form data
for key in table_values.keys():
table_values[key] = request.form.get(key)
# Write the updated values to the file
with open(VALUES_FILE_PATH, 'w') as file:
json.dump(table_values, file)
# Redirect back to the index page
return redirect('/')
# Pass the current table_values to the update.html template
return render_template('update.html', values=table_values)
if __name__ == '__main__':
app.run()