2012-06-27 70 views
0

我正在構建一個基於python的web服務器(是的,python是web服務器的不錯選擇,但這是我的唯一選擇我的目的還有另一個很好的選擇,例如PHP,但我被限制爲python)baseHTTPserver無法使用JavaScript庫

我使用ProtoVis進行一些數據可視化。 (一個基於JavaScript的可視化工具)

下面這段代碼工作,如果我只是複製並粘貼到一個測試文件並重新命名的.html(前提是我有它的旁邊提取protovis庫)

如果您想嘗試,得到它這裏https://github.com/mbostock/protovis/zipball/v3.3.1

<html> 
    <head> 
     <title>Area Chart</title> 
     <link type="text/css" rel="stylesheet" href="ex.css?3.2"/> 
     <script type="text/javascript" src="protovis/protovis.js"></script> 
     <style type="text/css"> 
     #fig { 
      width: 430px; 
      height: 225px; 
     } 
     </style> 
    </head> 
    <body> 
    <div id="center"> 
    <div id="fig"> 
    <script type="text/javascript+protovis"> 

var data = pv.range(0, 10, .1).map(function(x) { 
    return {x: x, y: Math.sin(x) + Math.random() * .5 + 2}; 
    }); 

/* Sizing and scales. */ 
var w = 400, 
    h = 200, 
    x = pv.Scale.linear(data, function(d) d.x).range(0, w), 
    y = pv.Scale.linear(0, 4).range(0, h); 

/* The root panel. */ 
var vis = new pv.Panel() 
    .width(w) 
    .height(h) 
    .bottom(20) 
    .left(20) 
    .right(10) 
    .top(5); 

/* Y-axis and ticks. */ 
vis.add(pv.Rule) 
    .data(y.ticks(5)) 
    .bottom(y) 
    .strokeStyle(function(d) d ? "#eee" : "#000") 
    .anchor("left").add(pv.Label) 
    .text(y.tickFormat); 

/* X-axis and ticks. */ 
vis.add(pv.Rule) 
    .data(x.ticks()) 
    .visible(function(d) d) 
    .left(x) 
    .bottom(-5) 
    .height(5) 
    .anchor("bottom").add(pv.Label) 
    .text(x.tickFormat); 

/* The area with top line. */ 
vis.add(pv.Area) 
    .data(data) 
    .bottom(1) 
    .left(function(d) x(d.x)) 
    .height(function(d) y(d.y)) 
    .fillStyle("rgb(121,173,210)") 
    .anchor("top").add(pv.Line) 
    .lineWidth(3); 

vis.render(); 

    </script> 
    </div> 
    </div> 
    </body> 
</html> 

但是,如果我在一個baseHTTPserver返回上面的代碼,它似乎並沒有工作。根據我的調查,似乎「protovis/protovis.js」中的圖書館沒有正確包含在內。

if url[0] == "/chart": 
    self.send_response(200) 
    self.send_header("Content-type","text/html") 
    self.end_headers() 
    self.wfile.write(chart()) 
    return 

其中chart()函數返回上面的行。

我在CentOS 6.2下使用python 2.6工作,有什麼特別的我需要在baseHTTPserver中做,以包含我正在使用的JavaScript庫嗎?使用Apache + PHP的相同代碼可以很好地迴應它們。

有什麼想法?

========================解決方案===================== ===

與Apache + PHP不同,BaseHTTPServer不會爲您放入該文件夾的任何內容提供服務。您必須自己動手,正如Matthew所描述的那樣,或者從不同的服務器提供protovis.js(甚至可能是運行在不同端口上的SimpleHTTPServer)。 - 瓦西里Faronov

下面

我必須做什麼來解決這個問題

見馬修·亞當斯的指示是添加另一種方法爲,處理JavaScript文件

if url[0] == "/protovis/protovis.js": 
    f = open("protovis/protovis.js","rb") 
    for each_line in f: 
     self.wfile.write(each_line) 
    return 

解決了do_GET()問題。

謝謝大家的解決方案,我真的很感激

+0

你確定這個:'