1

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.

JDS
  • 16,388
  • 47
  • 161
  • 224

6 Answers6

5

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" };

  • Yep, JSON is designed to be parsed natively by JavaScript, so is the proper format, and Python has a JSON library built in for writing it. – kindall Jul 18 '12 at 17:56
  • Is the line `var data = { json: "object here" };` written in Python and outputted to the .js file for my HTML to include? Sorry I'm just getting confused is all – JDS Jul 18 '12 at 20:01
  • 2
    Yes, you have it exactly, use the JSON encoder library @yorkrj mentioned at the point where ``{ json: "object here" }`` is in the text above. A simple string concatenation and then a write to the file. Be sure that your HTML file includes this file *before* your actual Javascript code file, or the ``data`` variable won't exist to manipulate from it's perspective. –  Jul 18 '12 at 20:10
1

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

Javier Segura
  • 658
  • 4
  • 13
1

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.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
1

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

Community
  • 1
  • 1
brian buck
  • 3,284
  • 3
  • 23
  • 24
1

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

yorkrj
  • 13
  • 1
  • 6
0

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()
3.3volts
  • 178
  • 1
  • 6