使用Python的D3.js,呈现一个空白页面

时间:2016-08-17 21:20:23

标签: python-3.x d3.js

设置:我试图用Python 3处理请求创建一个简单的D3 js演示。按照下面的步骤,我希望看到我用d3方法点击我的html页面时出现错误或某种HTML。相反,我收到一个空白页。

1)创建自定义HTTPRequestHandler类来处理GET请求

from http.server import BaseHTTPRequestHandler, HTTPServer
import os

class HTTPRequestHandler(BaseHTTPRequestHandler):

    #handle GET command
    def do_GET(self):
        rootdir = r'C:\Current_Working_Directory' #file location
        try:
            if self.path.endswith('.json'):
                f = open(rootdir + self.path, 'rb') #open requested file

                #send code 200 response
                self.send_response(200)

                #send header first
                self.send_header('Content-type','applicaon/json')
                self.end_headers()

                #send file content to client
                self.wfile.write(f.read())
                f.close()
                return

        except IOError:
            self.send_error(404, 'file not found')

def run():
    print('http server is starting...')

    #ip and port of server
    server_address = ('127.0.0.1', 80)
    httpd = HTTPServer(server_address, HTTPRequestHandler)
    print('http server is running...')
    httpd.serve_forever()

if __name__ == '__main__':
    run()

2)将上面的文件保存为server.py并运行python server.py并在$ rootdir中创建存储在data.json中的一些虚拟json数据并创建d3.html:

    <!DOCTYPE html>
    <html>
    <head>
    <title>D3 tutorial 10</title>
    <script src=”http://d3js.org/d3.v3.min.js”></script>
    </head>
    <body>
    <script>
    d3.json(“dummy.json”, function (error,data) {
        if (error) return console.warn(error);
        console.log(data)

    });
   </script>
   </body>
   </html>

4)打开网页,期望看到错误或d3已向服务器发送GET请求的一些迹象,但获取空白页。

0 个答案:

没有答案