2017-05-16 14 views
0

私はChatterbot 0.6と呼ばれるフレームワークで作業します。これはPythonでコード化されています。Django、JSからPythonを呼び出す方法は?

プロジェクトはウェブサイトとボットに基づいています。ボットはウェブサイト上に実装され、ユーザはチャットボックスを通じて彼と話すことができます。

ここまでは、私はPythonとWebサイトでチャットボットを行った。使命はDjangoのおかげでウェブサイトにボットを実装することです。私はいつもウェブサイトを実装することができましたが、今はボットの仕事をしなければなりません。

チャットボックスは、JSとPythonのボットでコード化されています。サイトをコーディングした私の仲間は、チャットボックスにボットの答えを促すJS機能を作った。

私の質問は、PythonをJS関数に実装する方法ですか?

var $msg_holder = undefined; 
$(document).ready(function(){ 
    $msg_holder = $("#message_holder"); 
}); 

/**Function that appends a String (msg) to a DOM element ($dom) 
*@param msg being the String to append (String object or literal string) 
*@param $dom being a JQobject/JQselection of a DOM element to append the msg to 
*/ 
function appendToDom(msg, $dom){ 
    $(msg).appendTo($dom); 
} 



/**Function that append a String (msg) to the Message Holder 
*@param msg being the String to append (String object or string literal) 
*/ 
function appendToMessageHolder(msg){ 
    appendToDom("<li>"+msg+"</li>", $("#message_holder")); 
} 

////////////////////////////////////////////// 

/**Function that gets the amount of messages currently on the page 
*@return the amount of messages currently on the page (unsigned integer) 
*/ 
function processMsgCount(){ 
    var $ul = $("#message_holder"); 
    var $li = $ul.children("li"); 

    return Math.abs($li.length);//overkill to ensure that a positive number is returned 
} 

////////////////////////////////////////////// 

/**Function that determines if it's the User's turn to give a sentence (in the scenario bot-->user user-->bot) 
*@return TRUE if it's indeed the user's turn, FALSE otherwise 
*/ 
function isUserTurn(){ 
    var ret = false; 

    var MSGcount = processMsgCount(); 

    //if odd, then it's the User's turn 
    if(MSGcount%2 != 0){ 
     ret = true 
    } 


    return ret; 
} 



/**Function that determines if it's the Bot's turn to give a sentence (in the scenario bot-->user user-->bot) 
*@return TRUE if it's indeed the bot's turn, FALSE otherwise 
*/ 
function isBotTurn(){ 
    return !isUserTurn(); 
} 

////////////////////////////////////////////// 

/**Function that displays the User's Input (wrapper function) 
*@param msg being the user input itself (String object or literal string) 
*/ 
function displayUserInput(msg){ 
    //condition is overkill, but meh 200% > 100% 
    if(isUserTurn()){ 
     appendToMessageHolder(msg); 
     scrollToLastMessage();//scroll down when new message 
    }else{ 
//  console.log("user tried to input a sentence when it was the bot's turn :x"); 
     console.error("user tried to input a sentence when it was the bot's turn :x"); 
    } 
} 



/**Function that displays the Bot's Response to the User's Input (or display the initial sentence) 
*@param msg being the bot's message/response (String object or literal string) 
*/ 
function displayBotResponse(msg){ 
    //condition is overkill, but meh 200% > 100% 
    if(isBotTurn()){ 
     appendToMessageHolder(msg); 
     scrollToLastMessage();//scroll down when new message 
    }else{ 
//  console.log("how the fuck did the bot try to give another message without user's input ?_?"); 
     console.error("how the fuck did the bot try to give another message without user's input ?_?"); 
    } 

} 

タグの悪い使用のために申し訳ありません - 」

どうもありがとう!

+4

「JS関数でPythonを実装する」ことはできません。あなたの場合は、Python(Djangoサーバ)とJavaScriptのAJAXリクエストを使って通信する必要があります。 – errata

答えて

0

Pythonはサーバー側で実行されますが、js関数は上記のようにクライアント側で実行されている必要があります。

jsからPythonを実行することは、私の情報としては不可能です。もっと重要なことは、達成しようとしていることの悪い習慣です。

どうすれば達成できますか?

Ajaxを参考にしてみてください。Ajaxリクエストを調べて、ボットや何かPythonをいくつかのイベントで実行したいと思ってください。

希望すると、これが役立ちます。

+0

私はこれを投稿する前に、私はここにAjaxが見えました、私はこの面にチェックします。しかし、例えば、質問に答える前に私のボットを初期化したいのであれば、HTMLページの最初にスクリプトをロードする必要がありますか? (Ajaxによるボットの初期化要求あり) – Darckoxe

+0

回答前にドキュメントにロードすることができます – burning

関連する問題