简体   繁体   English

如何在没有Web服务器的情况下将变量数据从Python传输到Javascript?

[英]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服务器。

Maybe you could write to a cookie and then access that via JavaScript? 也许您可以写一个cookie,然后通过JavaScript访问它? Similar to this SO answer here ? 类似于这里的答案吗?

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.

相关问题 如何将html视图数据或(Python)服务器数据传输到Angular或Javascript? - How to transfer html view data or (Python) server data to Angular or Javascript? 如何将变量从 Python Flask 转移到 JavaScript? - How can I transfer a variable from Python Flask to JavaScript? 如何将数据从变量传输到 redux 存储? - How to transfer data from variable to redux storage? 将变量值从服务器传输到javascript的最佳做法是什么? - What's the best practice to transfer a variable value to javascript from server? 如何使用 OnChange 将数据从 javascript 传输到 php - How to transfer data from javascript to php with OnChange 如何将数据从sql传输到javascript? - How to transfer data from sql to javascript? 我如何使用 AJAX 和 Flask 将诸如变量之类的信息从 javascript 传输到 python - How would I transfer information like a variable from javascript to python using AJAX and Flask 如何将数据从 node.js 传输到另一台服务器上的 JavaScript 客户端? - How would I transfer data from node.js to a JavaScript client on another server? 如何转移python对象以在JavaScript变量中使用? - How can I transfer a python object for use in a JavaScript variable? Web App-将Javascript变量传输到MySQL(JAVA) - Web App - Transfer Javascript variable to MySQL (JAVA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM