Trouble with Tornado and JavaScript Libraries

拜拜、爱过 提交于 2019-12-01 17:47:41

问题


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>

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

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.

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


回答1:


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")

  • Put the protovis-d3.2.js in your static directory

  • Refer to the file /static/protovis-d2.2.js in your HTML


来源:https://stackoverflow.com/questions/5167109/trouble-with-tornado-and-javascript-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!