How to dynamically output python dictionary to html in django?
Clash Royale CLAN TAG#URR8PPP
How to dynamically output python dictionary to html in django?
This is the data I get from socket dynamically and I would like to show it in a table like keys and values in html with django.
Received:
'power': 'ON', 'mode': 'AUTOMATIC', 'execution': 'ACTIVE', 'Xact': '235.70', 'Yact': '1468.86', 'Zact': '1.27', 'Xcom': '0.00', 'Ycom': '0.00', 'Zcom': '0.00', 'path_feedrate': '0.00', 'line': '1136849', 'Block': '1136849', 'program': '37262 S1 - .75 JET_imported_CNC.ORDn'
'comms': 'NORMAL', '': 'n2018-08-08T17:11:51.0384', 'Sspeed': '60000.00n'
'line': '1136860', 'Block': '1136860n'
'Xact': '236.17', 'Xcom': '909.70', 'path_feedrate': '909.70n'
'Xcom': '0.00', 'path_feedrate': '0.00', 'line': '1136872', 'Block': '1136872n'
'line': '1136883', 'Block': '1136883n'
'line': '1136895', 'Block': '1136895n'
'line': '1136906', 'Block': '1136906n'
'Xact': '236.52', 'Xcom': '677.44', 'path_feedrate': '677.44n'
'Xcom': '0.00', 'path_feedrate': '0.00', 'line': '1136918', 'Block': '1136918n'
'line': '1136929', 'Block': '1136929n'
'line': '1136941', 'Block': '1136941n'
and more more output.....
I tried to use this, but it did not work.
% block content %
<table>
% for key, value in devDict.items() %
<tr>
<td> key </td>
<td> value </td>
</tr>
% endfor %
</table>
% endblock content %
% block js %
<script type="text/python3" src="% static 'widgets/python/mtconnect.py'%"></script>
% endblock js %
And this is my python script, this is how i get the data:
import socket
HOST = "myHOST"
PORT = myPORT
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
buffer_size = 2048
print("Received: ")
while True:
devData = s.recv(buffer_size).decode("utf-8").split("|")
timeStamp = devData.pop(0)
devDict = dict(zip(*([iter(devData)]*2)))
print(devDict)
s.close()
there is no output when i try for loop with django. but when i just run my python code it gives me the output from above
– Asyl
Aug 8 at 17:33
.items()
is correct. Not .items
– Swift
Aug 8 at 17:34
.items()
.items
.items() is not correct, because i am getting this error: Could not parse the remainder: '()' from 'devDict.items()'
– Asyl
Aug 8 at 17:35
If you are loading a webpage then whatever data is available should be printed into the table and then rendered statically and passed to the client. If the data changed inbetween times, the page would not change without extra code that handles the live changes.
– Swift
Aug 8 at 17:40
2 Answers
2
Looks like you receive a list of dictionaries and firstly you need to iterate over the list. Smthn like:
<table>
% for line in devDict %
<tr>
% for key, value in line.items %
<td> key </td>
<td> value </td>
% endfor %
</tr>
% endfor %
</table>
The problem that i faced was that there was no possible way to render data from socket to browser, since there are two different protocols. I had to build first a bridge between them, socket to websocket. I did this by using: https://github.com/yankov/webtcp . and then i was able to render data from socket to browser.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what is the output for what you've tried?
– Lemayzeur
Aug 8 at 17:29