HTML / JQuery-> Python


                    

我是在Web开发环境中使用Python的新手(我以前只是用它来编写在命令行上运行的脚本)。 我希望通过AJAX从HTML页面发送一些输入参数,运行python脚本,然后将由我的python脚本生成的JSON对象发送回网页。 我试图使该示例运行,只是为了了解其工作原理:</ p>

http://davidderiso.com/post/6168199987/using- python-and-jquery#disqus_thread </ p>

我从test.py收到此错误消息:</ p>

 无法加载资源:服务器响应状态为501(不支持的方法('POST'))
</ code> </ pre>

我是否需要使用flask或bottle来运行此python脚本? 抱歉,我是这个菜的新手!</ p>

谢谢!</ p>
     </ div>

展开原文

原文

I'm new to using Python in a web development environment (I've previously just used it to write scripts I run on the command line). I'm hoping to send some input parameter from an HTML page via AJAX, run a python script, and then send back to the webpage a JSON object generated by my python script. I'm trying to get this example running just to get a sense for how this works:

http://davidderiso.com/post/6168199987/using-python-and-jquery#disqus_thread

I'm getting this error message from test.py:

Failed to load resource: the server responded with a status of 501 (Unsupported method     ('POST'))

Do I need to be running this python script with something like flask or bottle? Sorry, I'm a total newbie when it comes to this stuff!

Thanks!

2个回答


该教程应该向您展示如何在没有框架的情况下进行操作,但这还不完整。 例如,它没有讨论您应该如何首先访问HTML文件。 如果只是在浏览器中将其作为文件打开,则可能会收到该错误:您需要Apache提供服务(正如他指出的那样,它是为OSX内置的)- http:// localhost /my_html_file.html </ code> 可能</ em>起作用。</ p>
     </ div>

展开原文

原文

That tutorial is supposed to be showing you how to do it without a framework, but it's quite incomplete. For example, it doesn't talk about how you should be accessing the HTML file in the first place. If you simply open it as a file in your browser, you'd probably get that error: you need it to be served by Apache (which as he does point out is built in for OSX) - http://localhost/my_html_file.html might work.


我认为您应该使用 Tornado Web服务器! 使用Tornado,您将能够将python代码与html代码一起使用。</ p>

类似这样的事情... </ p>

  {%code%}
将熊猫作为pd导入
header ='使用龙卷风'
数据= pd.read_csv('country_data.csv')
....
{% 结束 %}

<!DOCTYPE html>
<头>
    

{{header}} </ title>
<样式>
...
<身体>
...
</ html>
</ code> </ pre>
     </ div>

展开原文

原文

I think you should use the Tornado Web Server ! Using Tornado you would be able to have your python code alongside your html code.

Something like this ...

{% code %}
import pandas as pd
header = 'Using Tornado'
data = pd.read_csv('country_data.csv')
....
{% end %}

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>{{ header }}</title>
<style>
...
<body>
...
</html>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问