简体   繁体   English

如何将Python脚本合并到我的网站中? 我需要一个框架吗?

[英]How can I incorporate Python scripts into my website? Do I need a framework?

I'm wondering if there's a good way for me to incorporate Python scripts into my current website. 我想知道我是否有一个很好的方法将Python脚本合并到我当前的网站中。

I have a personal website & my own server that I have been working with for a while. 我有一个个人网站和我自己的服务器,我已经工作了一段时间。 So far it's just been html / css / javascript. 到目前为止它只是html / css / javascript。 I have made a Python script in the past that uses another website's API to retrieve something that I would like to display on my website. 我以前制作了一个Python脚本,它使用另一个网站的API来检索我想在我的网站上显示的内容。 I've only used Python from the terminal to take input and spit out results. 我只使用终端上的Python来获取输入并吐出结果。 Is there a way for me to run a Python script from javascript through Ajax to get some content back? 有没有办法让我从javascript通过Ajax运行Python脚本来获取一些内容?

I don't really want to use a framework like Django or Flask because I feel as though those are mostly for entire projects. 我真的不想使用像Django或Flask这样的框架,因为我觉得这些框架主要用于整个项目。 I only want to use one Python script on one page for my website. 我只想在我的网站的一个页面上使用一个Python脚本。 Is this even something I should do? 这甚至是我应该做的事情吗? Any advice would be great. 任何建议都会很棒。

If you are using only javascript and don't feel like a framework is the solution, you'd better rewrite your python script using javascript. 如果你只使用javascript并且不觉得框架是解决方案,你最好使用javascript重写python脚本。 These two languages have a lot in common and most of the stuff are transferable. 这两种语言有很多共同之处,大部分内容都是可转移的。 Calling python from javascript would most likely not going to work that great. 从javascript调用python很可能不会那么好用。 Again, unless you share your python script(which is encouraged in SO because text only question does not quite fit in here), all answers are opinion based. 再说一遍,除非你分享你的python脚本(在SO中鼓励它,因为文本只有问题不适合这里),所有答案都是基于意见的。

I completly agree with you about Django, but I think you can give a chance to Flask, it is really light and I can be used for many porpouses. 我完全赞同你关于Django的事,但我认为你可以给Flask一个机会,它很轻,我可以用于许多人。 Anyway if you want to call a python scripts you need a way to call it. 无论如何,如果你想调用python脚本,你需要一种方法来调用它。 I think you need a "listener" for the script for example a service or a web service (for this reason I think Flask can be an really easy solution). 我认为您需要脚本的“监听器”,例如服务或Web服务(因此我认为Flask可以是一个非常简单的解决方案)。 Be careful about calling the script, a web service can be reachable from the frontend but this can not be done from a "standard" script. 注意调用脚本时,可以从前端访问Web服务,但这不能通过“标准”脚本完成。 My suggestion is take a look at Flask is more light that you think. 我的建议是看看Flask你认为更轻。

Give a chance to Flask! 给Flask一个机会! It's designed for the micro service that you just described. 它专为您刚才描述的微服务而设计。

Here is what you could do with less than 30 lines of code: 以下是您可以使用少于30行代码执行的操作:

app.py (flask file) app.py(烧瓶文件)

from flask import Flask, request
from flask import jsonify, render_template
app = Flask(__name__, template_folder='.', static_folder='.', static_url_path='')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/convert', methods=['POST'])
def convert():
    return jsonify(items=[
        ("whatever python function output that you have")
    ])

if __name__ == '__main__':
    app.run(debug=True)

index.html file index.html文件

        $.ajax({
            url: '/convert',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                var el = $('#req_result');

                for (let item of data.items) {
                   ### show your result here ###
                  }
                }

                $('#req_hide').hide();
                el.show();
            }
        });
      });

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

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