[AF] Reducing number of AJAX Requests
Hi there. I've been slowly improving my Flask web development skills, and I recently implemented some jQuery into my app in order to provide some "inline" form checking. I used [this as a reference](http://flask.pocoo.org/docs/0.11/patterns/jquery/#json-view-functions) when doing so.
**the route**
@app.route('/_form_counter', methods=['GET'])
def _form_counter():
if 'form_args' in request.args:
result = len(request.args['form_args'].splitlines())
else:
result = 0
return jsonify(result=result)
**the JS**
$(function() {
$('#form-count').text('Count: 0');
$('#sensor_ids').on('keyup', function() {
form_args = $('#sensor_ids').val()
$.getJSON($SCRIPT_ROOT + '/_form_counter', {
form_args: form_args,
}, function(data) {
$('#form-count').text('Count: ' + data.result)
});
});
});
Everything works the way I want it to, however, in order to keep the code modular and reusable, I have different JS files that perform different operations. The one I shared just puts a little counter at the bottom of the textarea which displays the count, but I have another one which checks if there are duplicates so you can correct it before you submit, and so on. Each of these little bits has its own JS file, and so whenever the form field is edited, it performs `n` requests, where `n` is the number of little modules I'm using.
I'm wondering if this is normal, because it feels a little cluttered. I feel like this wouldn't scale well, but I don't have any evidence to the contrary. I guess what I'm asking is if there's a way I can continue to keep these jQuery snippets modular, without having as many requests?
/r/flask
http://redd.it/5fsh9b
Hi there. I've been slowly improving my Flask web development skills, and I recently implemented some jQuery into my app in order to provide some "inline" form checking. I used [this as a reference](http://flask.pocoo.org/docs/0.11/patterns/jquery/#json-view-functions) when doing so.
**the route**
@app.route('/_form_counter', methods=['GET'])
def _form_counter():
if 'form_args' in request.args:
result = len(request.args['form_args'].splitlines())
else:
result = 0
return jsonify(result=result)
**the JS**
$(function() {
$('#form-count').text('Count: 0');
$('#sensor_ids').on('keyup', function() {
form_args = $('#sensor_ids').val()
$.getJSON($SCRIPT_ROOT + '/_form_counter', {
form_args: form_args,
}, function(data) {
$('#form-count').text('Count: ' + data.result)
});
});
});
Everything works the way I want it to, however, in order to keep the code modular and reusable, I have different JS files that perform different operations. The one I shared just puts a little counter at the bottom of the textarea which displays the count, but I have another one which checks if there are duplicates so you can correct it before you submit, and so on. Each of these little bits has its own JS file, and so whenever the form field is edited, it performs `n` requests, where `n` is the number of little modules I'm using.
I'm wondering if this is normal, because it feels a little cluttered. I feel like this wouldn't scale well, but I don't have any evidence to the contrary. I guess what I'm asking is if there's a way I can continue to keep these jQuery snippets modular, without having as many requests?
/r/flask
http://redd.it/5fsh9b
Media class in Django (https://docs.djangoproject.com/en/1.10/topics/forms/media/#form-assets-the-media-class)
I don't want to use bootstrap and I want to add custom CSS to my form. I think Media class (https://docs.djangoproject.com/en/1.10/topics/forms/media/#form-assets-the-media-class) might be of some help but I'm unable to understand how to use it in forms.py. Can someone please give me an explanation on how to integrate it with my form.
/r/django
https://redd.it/65vp20
I don't want to use bootstrap and I want to add custom CSS to my form. I think Media class (https://docs.djangoproject.com/en/1.10/topics/forms/media/#form-assets-the-media-class) might be of some help but I'm unable to understand how to use it in forms.py. Can someone please give me an explanation on how to integrate it with my form.
/r/django
https://redd.it/65vp20
reddit
Media class in Django... • r/django
I don't want to use bootstrap and I want to add custom CSS to my form. I think Media class...
[AF] Flask/SQL join question
Given the format below, how do I set up an SQLAlchemy query whereby I can search the approver or creator "column" along with a status. Obviously there is a join between Users and ISG somewhere, but I'm at a loss as to how to go about it.
Something like: models.ISG.filter(models.ISG.coordinator_status == 'New').filter(models.ISG.creator.name)", but that, you know, works.... ;)
#FORM
class searchForm(FlaskForm):
search_by = SelectField("Search Field", choices=[("approver", "Approved By"),("creator", "Created By")])
search_term = search_term = TextField('Search text')
status = SelectField("Coordinator Status", choices=[("New", "New"),("Pending", "Pending")])
#MODEL
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), index=True, unique=True)
class ISG(db.Model):
__tablename__ = 'isg'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(30), unique=False)
approver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
approver = db.relationship('Users', lazy='joined', foreign_keys=[approver_id])
creator_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
creator = db.relationship('Users', lazy='joined', foreign_keys=[creator_id])
/r/flask
https://redd.it/7s8ixp
Given the format below, how do I set up an SQLAlchemy query whereby I can search the approver or creator "column" along with a status. Obviously there is a join between Users and ISG somewhere, but I'm at a loss as to how to go about it.
Something like: models.ISG.filter(models.ISG.coordinator_status == 'New').filter(models.ISG.creator.name)", but that, you know, works.... ;)
#FORM
class searchForm(FlaskForm):
search_by = SelectField("Search Field", choices=[("approver", "Approved By"),("creator", "Created By")])
search_term = search_term = TextField('Search text')
status = SelectField("Coordinator Status", choices=[("New", "New"),("Pending", "Pending")])
#MODEL
class Users(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), index=True, unique=True)
class ISG(db.Model):
__tablename__ = 'isg'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.String(30), unique=False)
approver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
approver = db.relationship('Users', lazy='joined', foreign_keys=[approver_id])
creator_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
creator = db.relationship('Users', lazy='joined', foreign_keys=[creator_id])
/r/flask
https://redd.it/7s8ixp
reddit
[AF] Flask/SQL join question • r/flask
Given the format below, how do I set up an SQLAlchemy query whereby I can search the approver or creator "column" along with a status. Obviously...
Flask textbox fields?
Still very new to flask, so I'm sure there are probably some things that I could have done better/ didn't know you could do. Let me know if there's anything I should do for better practice.
​
I have a program that will take in a bunch of textbox inputs. After clicking a button, I want the result to be displayed. As of right now, the only thing I could really think of was:
form.result.label.text = result
However, that only changes the text box's label. Is there anyways I can have the result be posted into the textbox field? Or is there a label attribute I can use like a gui label? Thanks
#form.py
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField
from wtforms.validators import DataRequired
class information(FlaskForm):
studentName = StringField('Name', validators=[DataRequired()])
toeflScore = IntegerField('TOEFL', validators=[DataRequired()])
sopScore = IntegerField('SOP', validators=[DataRequired()])
lorScore = IntegerField('LOR', validators=[DataRequired()])
/r/flask
https://redd.it/bf47wz
Still very new to flask, so I'm sure there are probably some things that I could have done better/ didn't know you could do. Let me know if there's anything I should do for better practice.
​
I have a program that will take in a bunch of textbox inputs. After clicking a button, I want the result to be displayed. As of right now, the only thing I could really think of was:
form.result.label.text = result
However, that only changes the text box's label. Is there anyways I can have the result be posted into the textbox field? Or is there a label attribute I can use like a gui label? Thanks
#form.py
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField
from wtforms.validators import DataRequired
class information(FlaskForm):
studentName = StringField('Name', validators=[DataRequired()])
toeflScore = IntegerField('TOEFL', validators=[DataRequired()])
sopScore = IntegerField('SOP', validators=[DataRequired()])
lorScore = IntegerField('LOR', validators=[DataRequired()])
/r/flask
https://redd.it/bf47wz
reddit
r/flask - Flask textbox fields?
1 vote and 3 comments so far on Reddit