[英]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. 我正在自动生成一个本地HTML文件,我需要的所有相关数据都在Python脚本中。 Without a web server in place, I'm not sure how to proceed, because otherwise I think an AJAX/json solution would be possible.
没有适当的Web服务器,我不确定如何进行,因为否则我认为AJAX / json解决方案是可能的。
Basically in python I have a few list and dictionary objects that I need to use to create graphs using javascript and HTML. 基本上在python中,我需要使用一些列表和字典对象来使用javascript和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.
我拥有的一种解决方案(确实很糟糕)是使用字符串从字面上从Python内编写HTML / JS,然后保存到文件中。
What else could I do here? 我还能在这里做什么? I'm pretty sure Javascript doesn't have file I/O capabilities.
我很确定Javascript没有文件I / O功能。
Thanks. 谢谢。
You just need to get the data you have in your python code into a form readable by your javascript, right? 您只需要将python代码中的数据转换为JavaScript可读的形式,对吗?
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" };
为什么不仅仅采用数据结构,将其转换为JSON,然后编写一个
.js
文件,您的.html
文件中包括该文件,就是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 ? 您是否可以使用非常简单的Web服务器,例如: 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.
这样,如果您需要公开网站,则已经有了URL来设置适当的Web服务器。
You could use Python's JSON Encoder and Decoder library. 您可以使用Python的JSON编码器和解码器库。 This way you could encode your Python data into JSON format and include that in your HTML document.
这样,您可以将Python数据编码为JSON格式,并将其包含在HTML文档中。 You would then use Javascript in the HTML file to work with the JSON encoded data.
然后,您将在HTML文件中使用Javascript处理JSON编码的数据。
http://docs.python.org/library/json.html 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
; 要进行访问,您可以致电
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) 在JS端,您将像这样进行AJAX调用(假设使用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. 然后在Python端,您将把此文件与要在计算机上使用的html文件(index.html)放在同一目录中,并执行它。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.