简体   繁体   English

我可以在瓶子(框架)中使用javascript吗?

[英]Can I use javascript with bottle (framework)?

I'm trying to display a page of html using bottle (the python web framework). 我正在尝试使用bottle(python web framework)显示html页面。 The page has javascript embedded but it won't display it when I serve it with bottle. 该页面已嵌入javascript,但当我与Bottle一起使用时不会显示它。

The JS I'm using is EditArea , I can clean it up how I want and put it an html page that displays properly when I open the page in chrome. 我正在使用的JS是EditArea ,我可以按自己的意愿清理它,并将它放到一个html页面中,当我在chrome中打开该页面时,该页面可以正确显示。 But when I use bottle: 但是当我使用瓶子时:

@route('/edit')
def edit():
    return template('editarea')


@route('/edit_area')
def edit_area():
    send_file('example1.html', root='path/to/file/')

and go to http://localhost:8080/edit or /edit_area, I see the page without any of the fancy javascript features. 并转到http:// localhost:8080 / edit或/ edit_area,我看到的页面没有任何精美的javascript功能。

Eventually I want to hook this up (EditArea is text area and I'll be using it to accept code which hopefully I'll be able to run... but that's a separate issue...), but right now all it's supposed to do is display the page and the javascript. 最终,我想将其连接起来(EditArea是文本区域,我将使用它来接受希望能够运行的代码……但这是一个单独的问题……),但是现在所有这些都应该要做的是显示页面和javascript。 The JS is put in the html as simply as possible. JS尽可能简单地放在html中。 Those two blocks use different files but they are just copies of the same html file, one with .html and the other with .tpl extensions. 这两个块使用不同的文件,但它们只是同一个html文件的副本,一个带有.html,另一个带有.tpl扩展名。

<title>EditArea - the code editor in a textarea</title> 
    <script language="Javascript" type="text/javascript"     src="../edit_area/edit_area_full.js"></script> 
    <script language="Javascript" type="text/javascript"> 
        // initialisation
        editAreaLoader.init({

...and then it's all of the JS code (that I didn't write). ...然后是所有JS代码(我没有写过)。

In the file to start the server I import: route, run, debug, template, request, send_file, and error from bottle; 在启动我导入的服务器的文件中:路由,运行,调试,模板,请求,send_file和来自bottle的错误; and sqlite3; 和sqlite3; but that's all. 但是仅此而已。 Is there something else I should be including? 还有什么我应该包括的吗?

I've looked into the bottle documentation, and a few other places and it's either something really obvious that nobody bothers to write down or it's something that people just don't do... 我已经研究过瓶子的文档,以及其他一些地方,这是很明显的事实,没有人愿意写下来,或者是人们根本不这样做。

I was looking at pyjamas (it keeps coming up with different combinations of search queries involving "python" and "javascript"), but it looks like that just converts python into javascript. 我正在看睡衣(它不断提出涉及“ python”和“ javascript”的搜索查询的不同组合),但看起来只是将python转换成javascript。 I don't think that's what I want b/c the javascript is already javascript... 我不认为这就是我想要的B / C javascript已经是javascript ...

Thanks for any insight you might have. 感谢您的任何见解。

You need to create a view to serve static files, as described in Bottle documentation . 您需要创建一个视图来提供静态文件,如Bottle文档中所述

I suggest you to put all your static files (css, js, images) in a static folder next to your application. 我建议您将所有静态文件(css,js,图像)放在应用程序旁边的static文件夹中。 The view to serve static files would then look like this: 用于提供静态文件的视图如下所示:

from bottle import static_file    

@route('/static/:filename:')
def send_static(filename):
    return static_file(filename, root='./static/')

You would then include your .js file like this (using the path you'll have choosen of course): 然后,您将这样包含.js文件(使用您当然会选择的路径):

<script type="text/javascript" src="/static/edit_area/edit_area.js"></script>

If you're using the template system included in Bottle called SimpleTemplate then it does not support multi line strings and the templates get compiled to executable Python bytecode. 如果您使用的是Bottle中包含的名为SimpleTemplate的模板系统,则它不支持多行字符串,并且模板会被编译为可执行的Python字节码。 So its likely any Javascript would be stripped out. 因此,很可能会剥离所有Javascript。

The only way to include javascript in your page would be via script tags as you did for the "edit_area_full.js" file. 像在“ edit_area_full.js”文件中一样,在页面中包含javascript的唯一方法是通过脚本标记。

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

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