如何将vanilla python与AJAX一起使用?

时间:2016-07-16 18:27:42

标签: javascript jquery python ajax cgi

我正在开展一个项目,该项目需要向我展示智能搜索建议,就像谷歌搜索一样,但我自己的数据库。 我研究了一下网络,发现了AJAX。作为一名蟒蛇爱好者,我不喜欢PHP的外观和感觉。 但问题是,99%的文档,您在网上为python CGI编程找到的示例和参考都是基于Web框架的。 好吧,我很乐意使用一个Web框架,但我的伙伴也在使用PHP,并在文档中查看部署和Python Web应用程序与其他一些东西只会让我发疯。 所以我使用内置cgi模块的vanilla python。 我不知道我的AJAX脚本和python运行良好,但后来我改变了搜索算法一点点,现在它无法正常工作。 我尝试使用局部变量进行测试并猜测它的工作原理,但它在我的网页上无效。

这是我的网页:

<!DOCTYPE html>
<html>
<head>
<title>Search</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!-- Only When there is no internet connection in my house/anywhere -->
<!-- <script type="text/javascript" src = 'js/jquery3.js'></script> -->
<style type="text/css">
    body{
        margin:0px;
        font-family:arial,sans-serif;
    }
    /* Div Ids Begin */
    #parentHeader{
        background-color:#6B1C0D;
        width:100%;
    }
    #childHeader{
        padding-top:20px;
        padding-bottom:30px;
        margin-left:22%;
    }
    /* Div Ids End */
    /* The text field */
    #searchBox{
        width:70%;
        resize:none;
        font-size:15px;
    }
    /* For the box with suggestions */
    #ajaxResults{
        background: #ffffff;
        padding: 5px 10px;
        max-height: 400px;
        overflow: auto;
        position: absolute;
        z-index: 99;
        border: 1px solid #A9A9A9;
        border-width: 0 1px 1px 1px;
    }
</style>
</head>
<body>

<div id="parentHeader">
<div id='childHeader'>
    <input type="textbox" id="searchBox" name="searchQuery" placeholder="Search Here..."><br />
    <div id="ajaxResults"></div>
</div>
</div>
<!-- JavaScript Begins -->
<script type="text/javascript">
// Make the ResultBox Hidden
$('#ajaxResults').hide();
// On Every Key Press
$('#searchBox').keyup(function() {
// Value Obtained
val = $('#searchBox').val();
// Check Whether its empty or not
if(val.length == 0){
    $('#ajaxResults').hide();
    $('#ajaxResults').html();
}else{
    // Performs flawlessly , isnt it?
    // Afterall it took me hours to find out how can I use Vanilla Python with AJAX
    // Just if I forget my Hardwork
    $('#ajaxResults').show();
    $('#ajaxResults').load('/cgi-bin/suggest.py',{'q':val});
}
});
</script>
</body>
</html>

我的python脚本:

#!/usr/bin/python3


#CGI Thingies Begin

import cgi
import cgitb

cgitb.enable()


form = cgi.FieldStorage()
reqVal = form.getvalue('q')

#CGI Thingies End

# reqVal = "Maths T"


text_raw = open("suggestions.txt","r") ##TO_DO : Change input from text file to Database
text = text_raw.read()
suggestions = text.split('\n')


retStr = "" #String which is to be returned

queryLength = len(reqVal)

retLen = len(reqVal.split(' '))

# pick a suggestion from suggestions
# get the reqVal and extract the word which is going to be typed
# take the whole string with the suggestion but only return upto the word
# which is going to be typed but not completly written
# if the user is going to type a word show the available suggestion upto the next word
# The principle loop which does the sorting



for suggestion in suggestions:
    temp = suggestion
    suggestion = suggestion[:queryLength]
    if(suggestion == reqVal):
        tempArr = temp.split(' ')
        for i in range(0,retLen,1):
            retStr = retStr + tempArr[i] + " "
        retStr = retStr + " "

# Function to sort out Duplis

def sortOut(checkVar):
    seen = set()
    seenAdd = seen.add
    return [x for x in checkVar if not (x in seen or seenAdd(x))]
    pass

checkStr = retStr.split(' ')

retVar = sortOut(checkStr)
retStr = ""

for var in retVar:
    retStr = retStr +var+ '<br>'

retStr = retStr[:-4] #To remove that extra <br>

#Prints

print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<body>')
print (retStr)
print ('</body>')
print ('</html>')

由于

0 个答案:

没有答案