I have a simple Flask app that contains a single variable called variable:
from flask import Flask, render_template
app = Flask(__name__)
variable = 100
@app.route('/', methods=['GET'])
@app.route('/index/', methods=['GET'])
def index():
return render_template('index.html', variable=variable)
def main():
app.run(debug=True, port=5000)
if __name__ == '__main__':
main()
The index.html file is as follows:
variable = {{ variable }}
<button onclick="setVariable(101);">
set variable to 101
</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function setVariable(variable) {
console.log(`variable = ${variable}`);
}
</script>
Is there any way to set the variable value within the setVariable function? i.e. such that the value of variable is set to 101 in this example.
Basically the end goal is to dynamically set the value of variable using a button, which should furthermore update the variable = {{ variable }} line in the index.html file.
EDIT
The following implies you can not do this and must use an ajax request or something similar.
/index/variable=101. Perhaps just posting forms back usingurl_for(index, form=some_form)would be the way to go?variablecould be (and is) an object ... just putting anintdown for simplicity