Created
May 5, 2017 01:55
-
-
Save rschutjens/4d8302071f467d3395b87dad3993fc3c to your computer and use it in GitHub Desktop.
dynamic update of selectField options Flask Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import json, make_response, render_template_string, request | |
from flask_wtf import Form | |
from wtforms import IntegerField, SelectField | |
from wtforms.validators import DataRequired | |
template = """ | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
</head> | |
<body> | |
<form action="" method="post" id="selectDevice" name="device"> | |
{{ form.hidden_tag() }} | |
<p> | |
Nummber of Devices: {{ form.selectAmount(size=1) }} | |
Select device: {{form.deviceAddress() }} | |
</p> | |
</form> | |
<script type="text/javascript" charset="utf-8"> | |
$("#deviceAddress").click(function(){ | |
$.ajax({ | |
url: '/selectform', | |
type: 'POST', | |
data: $('#selectDevice').serialize(), | |
success: function(selectOptions){ | |
$("#deviceAddress").empty(); | |
for (var i = 0; i < selectOptions.length; i++){ | |
$("#deviceAddress").append( | |
$("<option></option>") | |
.attr("value", selectOptions[i][0]) | |
.text(selectOptions[i][1]) | |
); | |
} | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> | |
""" | |
app = Flask(__name__) | |
app.config['WTF_CSRF_ENABLED'] = True | |
app.config['SECRET_KEY'] = 'you-will-never-guess' | |
class SelectDevice(Form): | |
deviceAddress = SelectField('device number') | |
selectAmount = IntegerField('amount devices', | |
[DataRequired()], | |
default = 3) | |
def __init__(self, *args, **kwargs): | |
super(SelectDevice, self).__init__(*args, **kwargs) | |
defaultAmount = self.selectAmount.data | |
self.deviceAddress.choices = [('device{}'.format(i), i) for i in range(defaultAmount)] | |
@app.route('/') | |
@app.route('/selectform', methods=['GET']) | |
def select(): | |
form = SelectDevice() | |
return render_template_string(template, form=form) | |
@app.route('/selectform', methods=['POST']) | |
def updateselect(): | |
deviceAmount = int(request.form.get('selectAmount')) | |
choices = [('device{}'.format(i), i) for i in range(deviceAmount)] | |
response = make_response(json.dumps(choices)) | |
response.content_type = 'application/jsons' | |
return response | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
</head> | |
<body> | |
<form action="" method="post" id="selectDevice" name="device"> | |
{{ form.hidden_tag() }} | |
<p> | |
Nummber of Devices: {{ form.selectAmount(size=1) }} | |
Select device: {{form.deviceAddress() }} | |
</p> | |
</form> | |
<script type="text/javascript" charset="utf-8"> | |
$("#deviceAddress").click(function(){ | |
$.ajax({ | |
url: '/selectform', | |
type: 'POST', | |
data: $('#selectDevice').serialize(), | |
success: function(selectOptions){ | |
$("#deviceAddress").empty(); | |
for (var i = 0; i < selectOptions.length; i++){ | |
$("#deviceAddress").append( | |
$("<option></option>") | |
.attr("value", selectOptions[i][0]) | |
.text(selectOptions[i][1]) | |
); | |
} | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment