简体   繁体   中英

How to transfer variable data from Python to Javascript without a web server?

I'm working on automatically generating a local HTML file, and all the relevant data that I need is in a Python script. Without a web server in place, I'm not sure how to proceed, because otherwise I think an AJAX/json solution would be possible.

Basically in python I have a few list and dictionary objects that I need to use to create graphs using javascript and HTML. One solution I have (which really sucks) is to literally write HTML/JS from within Python using strings, and then save to a file.

What else could I do here? I'm pretty sure Javascript doesn't have file I/O capabilities.

Thanks.

You just need to get the data you have in your python code into a form readable by your javascript, right?

Why not just take the data structure, convert it to JSON, and then write a .js file that your .html file includes that is simply var data = { json: "object here" };

What do you thing about using some Templating system? It will fit your needs.

I know you've specifically mentioned "without a web-server", but unless you want to really go out of your way, and over-complicate this, and restrict flexibility for future use:-

Could you not use a very simple webserver such as: http://docs.python.org/library/simplehttpserver.html ? That way, should you need to expose the site, you've got the URL's already in place to set-up a proper webserver.

Maybe you could write to a cookie and then access that via JavaScript? Similar to this SO answer here ?

You could use Python's JSON Encoder and Decoder library. This way you could encode your Python data into JSON format and include that in your HTML document. You would then use Javascript in the HTML file to work with the JSON encoded data.

http://docs.python.org/library/json.html

If this only needs to be for localhost, you could do something like the following. To access, you would make a call to http://localhost:8080/foo ; this can cause some issues due to Cross Site Injection Protection, however; these are readily solved by Googling around.

On the JS side, you would make an AJAX call like this (assuming jQuery)

$.ajax('http://localhost:8080/foo', function (data) {console.log(data)});

And then on the Python side you would have this file in the same directory as the html file you are seeking to use (index.html) on your computer, and execute it.

import BaseHTTPServer
import json

class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_GET(self):
            desiredDict = {'something':'sent to JS'}
            if self.path == '/foo':
                self.send_response(200)
                self.send_header("Content-type", "application/json")
                self.end_headers()
                self.wfile.write(json.dumps(desiredDict))
            else: 
                if self.path == '/index.html' or self.path == '/':
                    htmlFile = open('index.html', 'rb')
                    self.send_response(200)
                    self.send_header("Content-type", "text/html")
                    self.send_header("Access-Control-Allow-Origin","http://localhost:8080/")
                    self.end_headers()
                    self.wfile.write(htmlFile.read())
                else:
                    self.send_error(404)

server = BaseHTTPServer.HTTPServer(('',8080), WebRequestHandler)
server.serve_forever()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM