简体   繁体   English

Tornado和JavaScript库的问题

[英]Trouble with Tornado and JavaScript Libraries

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. 我正在尝试使用Tornado Web服务器编写一个简单的python Web应用程序,并且无法使用我需要的JavaScript库。 I wanted to use the Protovis JavaScript plotting library, so I added the following 'Hello World' code snippet to my template.html: 我想使用Protovis JavaScript绘图库,所以我将以下“Hello World”代码片段添加到我的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>

Whenever I run the webserver, however, and try accessing the page, I get the following error at the console: 但是,每当我运行Web服务器并尝试访问该页面时,我在控制台上都会收到以下错误:

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

The protovis.js file is in the same directory as my server.py file, and all its permissions are set correctly. protovis.js文件与我的server.py文件位于同一目录中,并且其所有权限都已正确设置。 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文件时遇到同样的错误,所以我知道protovis.js文件没有问题,但是Tornado服务器的路由有问题。

Does anyone know how I can properly src this JavaScript code, thanks. 有谁知道我怎么能正确地src这个JavaScript代码,谢谢。

You should read the documentation about static files . 您应该阅读有关静态文件的文档。

In particular, the standard way is to: 特别是,标准方法是:

  • 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”)

  • Put the protovis-d3.2.js in your static directory protovis-d3.2.js放在静态目录中

  • Refer to the file /static/protovis-d2.2.js in your HTML 请参阅HTML中的文件/static/protovis-d2.2.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM