2016-12-03 45 views
0

我爲我的項目在python(2.7)中構建了一個推薦引擎但現在我想從HTML頁面讀取用戶的輸入,將此信息傳遞給python中的推薦引擎,並將結果顯示回HTML頁。有沒有更簡單的方法來實現這一點?如何將Python與前端HTML集成?

+0

您到目前爲止嘗試過了什麼?您所做的一些示例代碼將有助於我們向其提供輸入。 – serk

回答

0

你可能想運行一個像Twisted這樣的網絡服務器,或者使用像Django這樣的框架。您可以使用其中的一種來運行服務器,呈現您的HTML並直接與您的推薦引擎對用戶輸入進行響應。

0

可以使用實現了一個簡單的服務器使用WSGI類似這樣的例子

from wsgiref.simple_server import make_server 

# Every WSGI application must have an application object - a callable 
# object that accepts two arguments. For that purpose, we're going to 
# use a function (note that you're not limited to a function, you can 
# use a class for example). The first argument passed to the function 
# is a dictionary containing CGI-style environment variables and the 
# second variable is the callable object (see PEP 333). 
def hello_world_app(environ, start_response): 
    status = '200 OK' # HTTP Status 
    headers = [('Content-type', 'text/plain')] # HTTP Headers 
    start_response(status, headers) 

    # The returned object is going to be printed 
    return ["Hello World"] 

httpd = make_server('', 8000, hello_world_app) 
print "Serving on port 8000..." 

# Serve until process is killed 
httpd.serve_forever() 
0

更好的使用Python Web框架,如Flask(其爲基於Python的Web應用程序的輕質框架)或Django(如果您計劃使用python構建完整的web應用程序)