0

I'm new to python and javascript so I don't know much about their data sharing capabilities. Below is the dictionary (whose data I want to send) in a database file I have called database.py. How can I take this data and send it to my javascript file "Calendar.js" so I can use it in a function later?

agenda = {'id' : num,
                  'eventName' : eventName,
                  'eventType' : eventType,
                  'location' : location,
                  'date' : date,
                  'time' : time,
                  'compName' : compName,
                  'attire' : attire,
                  'additional' : additional}

1 Answer 1

1

As a general rule, consider that JSON (JavaScript Object Notation) is the goto-format if you want to exchange data with javascript.

From Python, you can use the json module to generate such data.

Example:

d = {"name":"interpolator",
     "children":[{'name':key,"size":value} for key,value in sample.items()]}
j = json.dumps(d, indent=4)
f = open('sample.json', 'w')
print >> f, j
f.close()

Now, for the actual communication with javascript, you can either write the JSON in a file and process it asynchronously, or use something like protobuf/grpc.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, so I use this in my python file but how do I retrieve that data in my javascript file?
Unless you're running a javascript back-end (for example, Node.js), you will have a hard time getting the content of a file. You could probably use websockets for communication between the back-end and the website's front-end.
Ah ok that makes sense. Does the file it is written in matter? Like should it been done in my html page or the python database etc.?
Well, it depends. If you want to load that data statically (i.e., load it once whenever someone loads the page), you can probably put it directly in your html page. You can also store it in a database and have javascript in the page load it. To be honest the format/way of storing it doesn't matter much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.