of my dashboard_bg.html template:
**dashboard_bg.html:**
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p class="h5">The result of job ID <strong id='job'></strong> is: <strong id='result'></strong></p>
</div>
<script type="text/javascript">
(function(){
var getJobProgress = function(jobId, csrf_token) {
$.ajax({
url: "/getjobresult/",
type: "POST",
data: {
"jobId" : jobId,
"csrfmiddlewaretoken" : csrf_token
}
}).done(function(data){
console.log(data);
var jsonData = JSON.parse(JSON.stringify(data));
result = jsonData['result'];
if(result == null){
console.log('Result is still null');
// pass
} else {
$('#result').text(result);
$('#job').text(jobId);
}
});
}
i=0;
// Check every second to see if we have a result yet, for 10 seconds
setInterval(function(){
if(i<=10){
if(i%1 == 0){
console.log('Checking progress!');
getJobProgress('{{ DATA1.id }}', '{{ csrf_token }}') // Note DATA1.id is now the id of the job from RQ, since DATA1 is a job object (technically it's still a dict but not the one I need)
}
} else {
clearInterval();
}
i++;
}, 1000);
})();
</script>
ANNNNNNYWAY after all that, the good news is that it (sort of) works. The dashboard_bg template is rendered and what I end up with (for now) is, initially, text that says "The result of job ID is:" ... and after a few seconds of ajax calls and the first function finishes, something like this:
The result of job ID 8b4f11f3-cb50-488b-a1cb-88166f700463 is: [object Object]
In the end the thing is working fine. I looked in the console where the object is logged and the return data from the long function is in there. **The question I have is now that it is a JSON object, how do I access it in my original form using the {{ variable }} syntax?** It's extremely important that I'm able to do this because I have thousands of lines of code that reference the template variables this way, in various files, javascripts and countless other things. So is there a way to still be able to access the contents of the returned JSON object like {{ DATA1.thing.deeper_thing.some_other_thing }}? If not, how do I return the context data from the getJobResults() function as the original django template variable?
Hopefully that all makes sense. Sorry for such a long post but I wanted to be thorough. Let me know if you need more details! THANK YOU!!!
/r/django
https://redd.it/67kcd0
**dashboard_bg.html:**
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p class="h5">The result of job ID <strong id='job'></strong> is: <strong id='result'></strong></p>
</div>
<script type="text/javascript">
(function(){
var getJobProgress = function(jobId, csrf_token) {
$.ajax({
url: "/getjobresult/",
type: "POST",
data: {
"jobId" : jobId,
"csrfmiddlewaretoken" : csrf_token
}
}).done(function(data){
console.log(data);
var jsonData = JSON.parse(JSON.stringify(data));
result = jsonData['result'];
if(result == null){
console.log('Result is still null');
// pass
} else {
$('#result').text(result);
$('#job').text(jobId);
}
});
}
i=0;
// Check every second to see if we have a result yet, for 10 seconds
setInterval(function(){
if(i<=10){
if(i%1 == 0){
console.log('Checking progress!');
getJobProgress('{{ DATA1.id }}', '{{ csrf_token }}') // Note DATA1.id is now the id of the job from RQ, since DATA1 is a job object (technically it's still a dict but not the one I need)
}
} else {
clearInterval();
}
i++;
}, 1000);
})();
</script>
ANNNNNNYWAY after all that, the good news is that it (sort of) works. The dashboard_bg template is rendered and what I end up with (for now) is, initially, text that says "The result of job ID is:" ... and after a few seconds of ajax calls and the first function finishes, something like this:
The result of job ID 8b4f11f3-cb50-488b-a1cb-88166f700463 is: [object Object]
In the end the thing is working fine. I looked in the console where the object is logged and the return data from the long function is in there. **The question I have is now that it is a JSON object, how do I access it in my original form using the {{ variable }} syntax?** It's extremely important that I'm able to do this because I have thousands of lines of code that reference the template variables this way, in various files, javascripts and countless other things. So is there a way to still be able to access the contents of the returned JSON object like {{ DATA1.thing.deeper_thing.some_other_thing }}? If not, how do I return the context data from the getJobResults() function as the original django template variable?
Hopefully that all makes sense. Sorry for such a long post but I wanted to be thorough. Let me know if you need more details! THANK YOU!!!
/r/django
https://redd.it/67kcd0
reddit
Need help with Django/Ajax/RQ (long) • r/django
Hey all, I'll try to make this as simple but also as detailed as possible. I have an app running on Heroku which has a couple functions that take...
[AF] How do I send data route to route, in order to maintain a dynamic form?
This question is probably not asked correctly. I'll try to explain better below. I am also new to python and flask.
I am creating a flask web interface for a simple inventory system we have. I'm trying to have the following interaction:
USER: <Inputs part number into appropriate field>
SERVER: <Checks to see if on file>
if on file
SERVER: "Place bin on scale."
USER: <Places bin on scale>
*SCALE: <Weighs item and sends value to server>
*My issue
Here is my issue: the scale data sent is not within scope of the "inventory" route. Is there a better way to go about this?
**My Direction:**
I want the user to have the form readily available for another form input if necessary. I also want to print the new data with some data pulled from the database after the item is weighed.
**app/routes.py**
from flask import render_template, flash, redirect, url_for, request, jsonify
from app import app, db
from app.forms import LoginForm, RegistrationForm, PrintLabelForm, \
InventoryForm1, InventoryForm2
from flask_login import current_user, login_user, logout_user, login_required
from app.models import *
import scale as s
@app.route('/inventory', methods=['GET', 'POST'])
@login_required
def inventory():
form = InventoryForm1()
# if form.validate_on_submit(): # Checks form submission syntax validity
if "submit" in request.form: # Checks form submission syntax validity
item = Items.query.filter_by(ItemCode=form.itemCode.data).first()
measurement = Measurements.query.filter_by(partNumber=form.itemCode.data).first()
# Checks if part number field is correct
if item is None: # No input for the field
flash('Item not on file.')
return redirect(url_for('inventory'))
elif measurement is None: # No current measurements available
flash('No previous measurements found for, ' + item.ItemCodeDesc + '.')
flash('Place empty container on scale.')
# while container is not containerChange and keyboardIdle:
# await item data input
else: # Generate Item Report
flash('Place bin on scale.')
# User clicks "Weigh Item"
return render_template('inventory.html', title='Inventory', form=form)
@app.route('/weighItem', methods=['GET', 'POST'])
@login_required
def weighItem():
weight = s.runScale("getWeight", 0)
return jsonify(weight=weight)
**app/templates/base.html**
<script>
function weighItem() {
$.post('/weighItem', {
}).done(function(response) {
$("#result").text(response.weight);
alert(response.weight);
}).fail(function() {
$.text("Error: Could not contact server.");
});
}
</script>
**app/templates/inventory.html**
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Inventory</h1>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form) }}
<a href="javascript:weighItem()" id="weighItem">Weigh Item</a>
<p id="result"> </p>
</div>
</div>
{% endblock %}
**Some Notes:**
I am using the [https://github.com/tatobari/hx711py](https://github.com/tatobari/hx711py) for a scale.
/r/flask
https://redd.it/8h0ur9
This question is probably not asked correctly. I'll try to explain better below. I am also new to python and flask.
I am creating a flask web interface for a simple inventory system we have. I'm trying to have the following interaction:
USER: <Inputs part number into appropriate field>
SERVER: <Checks to see if on file>
if on file
SERVER: "Place bin on scale."
USER: <Places bin on scale>
*SCALE: <Weighs item and sends value to server>
*My issue
Here is my issue: the scale data sent is not within scope of the "inventory" route. Is there a better way to go about this?
**My Direction:**
I want the user to have the form readily available for another form input if necessary. I also want to print the new data with some data pulled from the database after the item is weighed.
**app/routes.py**
from flask import render_template, flash, redirect, url_for, request, jsonify
from app import app, db
from app.forms import LoginForm, RegistrationForm, PrintLabelForm, \
InventoryForm1, InventoryForm2
from flask_login import current_user, login_user, logout_user, login_required
from app.models import *
import scale as s
@app.route('/inventory', methods=['GET', 'POST'])
@login_required
def inventory():
form = InventoryForm1()
# if form.validate_on_submit(): # Checks form submission syntax validity
if "submit" in request.form: # Checks form submission syntax validity
item = Items.query.filter_by(ItemCode=form.itemCode.data).first()
measurement = Measurements.query.filter_by(partNumber=form.itemCode.data).first()
# Checks if part number field is correct
if item is None: # No input for the field
flash('Item not on file.')
return redirect(url_for('inventory'))
elif measurement is None: # No current measurements available
flash('No previous measurements found for, ' + item.ItemCodeDesc + '.')
flash('Place empty container on scale.')
# while container is not containerChange and keyboardIdle:
# await item data input
else: # Generate Item Report
flash('Place bin on scale.')
# User clicks "Weigh Item"
return render_template('inventory.html', title='Inventory', form=form)
@app.route('/weighItem', methods=['GET', 'POST'])
@login_required
def weighItem():
weight = s.runScale("getWeight", 0)
return jsonify(weight=weight)
**app/templates/base.html**
<script>
function weighItem() {
$.post('/weighItem', {
}).done(function(response) {
$("#result").text(response.weight);
alert(response.weight);
}).fail(function() {
$.text("Error: Could not contact server.");
});
}
</script>
**app/templates/inventory.html**
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>Inventory</h1>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form) }}
<a href="javascript:weighItem()" id="weighItem">Weigh Item</a>
<p id="result"> </p>
</div>
</div>
{% endblock %}
**Some Notes:**
I am using the [https://github.com/tatobari/hx711py](https://github.com/tatobari/hx711py) for a scale.
/r/flask
https://redd.it/8h0ur9
GitHub
GitHub - tatobari/hx711py: HX711 Python Library for Raspberry Pi.
HX711 Python Library for Raspberry Pi. Contribute to tatobari/hx711py development by creating an account on GitHub.