0

I am working on a piece of Python code (learning as I go), the output of which needs to be passed onto a Javascript application using JSON. Is it possible pass an image (in whatever format) onto the Javascript application using JSON? I have no idea, but if possible it is something I need to learn how to do.

Ultimately, this python code will run inside the Javascript application.

traggatmot
  • 1,423
  • 5
  • 26
  • 51
  • If you can encode the image data as character data, then yes. – Felix Kling Jan 06 '15 at 05:16
  • 1
    It is indeed possible. However, that is as much detail as I can bring to your question, given how detailed the question itself is. Python code cannot run inside a JavaScript application, but can be invoked by it. Do you want to make a static image file by Python? Do you invoke Python CGI? FastCGI? WSGI? What kind of server do you have at your disposal? Can you install your own daemons? Do you have Apache or nginx with Passenger? Do you already have a Python webapp running and supporting your JavaScript app? Is your image vector or bitmap? – Amadan Jan 06 '15 at 05:17
  • Right, so the Python code will be invoked by the javascript application - sorry about not knowing the proper terminology. I want to make several static images, graphs really, that the Javascript application will need to display. I'm not in charge of invoking the python code - when my nugget of code is finished it will be passed off to the developers of the javascript application. Any suggestions you have given my lack of knowledge are insanely helpful - even if the lack of knowledge is frustrating you and hindering your response. – traggatmot Jan 06 '15 at 05:21
  • No worries, as long as *some* communication is done. :) If all you need is for Python to generate static images that will be served unchanged for the next years, months or even hours, the JavaScript side of the matter is pretty much irrelevant: just dump the image to the file and put the file somewhere the web server can see it. – Amadan Jan 06 '15 at 05:49
  • So, an image can then definitely be stored in a JSON script? Excellent. I'll start investigating how to do that. – traggatmot Jan 06 '15 at 05:54
  • You could send the entire image file as binary data via JSON by reading it into a `bytearray` and then decoding that into `latin1`. The receiving side would then have to "encode" that from `latin1` to get the original data back. See the code in [this answer](http://stackoverflow.com/a/22621777/355230) for an all-Python example. – martineau Jan 06 '15 at 08:45
  • The JavaScript makes an Ajax-request to the Python-script on the server. The Python script writes the image to a file on the server, and gives the URL back to the JavaScript. The JavaScript then creates an ``-tag, and sets the `src`-attribute to that URL. – Markus Jarderot Jan 06 '15 at 08:46

0 Answers0