本文介绍了Tornado和JavaScript库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tornado Web服务器编写一个简单的python Web应用程序,并且无法使用我需要的JavaScript库。我想使用Protovis JavaScript绘图库,所以我将以下'Hello World'代码段添加到我的template.html:

I'm trying to write a simple python web application using the Tornado web server and am having trouble using the JavaScript libraries I need. I wanted to use the Protovis JavaScript plotting library, so I added the following 'Hello World' code snippet to my template.html:

<script type="text/javascript" src="/protovis-d3.2.js"></script>
<script type="text/javascript+protovis">
new pv.Panel()
    .width(150)
    .height(150)
    .anchor("center")
    .add(pv.Label)
        .text("Hello, world!")
        .root.render();
</script>

然而,每当我运行网络服务器并尝试访问该页面时,我都会收到以下错误: console:

Whenever I run the webserver, however, and try accessing the page, I get the following error at the console:

WARNING:root:404 GET /protovis-d3.2.js (127.0.0.1) 0.46ms

protovis.js文件与我的server.py文件位于同一目录中,并且所有权限都已正确设置。我在尝试使用src和JavaScript文件时遇到同样的错误,所以我知道protovis.js文件没有问题,但是Tornado服务器的路由有问题。

The protovis.js file is in the same directory as my server.py file, and all its permissions are set correctly. I get the same error when trying to src and JavaScript file so I know there isn't a problem with the protovis.js file, but something with the Tornado server's routing.

有谁知道如何正确地src这个JavaScript代码,谢谢。

Does anyone know how I can properly src this JavaScript code, thanks.

推荐答案

您应该阅读有关。

特别是,标准方法是:


  • 在应用程序的根目录中创建一个静态目录

  • Create a 'static' directory in the root of your application

将以下内容添加到您的应用程序设置:

Add the following to your application settings:

static_path:os.path.join(os.path.dirname( file ),static)

"static_path": os.path.join(os.path.dirname(file), "static")

protovis-d3.2.js 放入静态目录

这篇关于Tornado和JavaScript库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:23
查看更多