[Python] TypeError: kernel.execute is not a function

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    Run the following code in the jupyter cell to interact with python and jquery.The code is as follows:

    labels = []
    from IPython.display import HTML,display
    display(HTML("""
    <div name="tweetbox"">
    Instructions: Click in textbox. Enter a 1 if the tweet is relevant, enter 0 otherwise. </br>
    Tweet: <div id="tweet_text" value="text"></div><br>
    <input type=text id="capture"></input><br>
    </div>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function(){
    function set_label(label){
    try {
    var kernel = IPython.notebook.kernel;
    kernel.execute("labels.append("+ JSON.stringify(label) +")");
    load_next_tweet();
    }catch(error){
    alert(error)
    }
    }
    function load_next_tweet(){
    var code_input = "get_next_tweet()";
    var kernel = IPython.notebook.kernel;
    var callbacks = {"iopub":{"output":handle_output}};
    kernel.execute(code_input, callbacks, {silent:false});
    }
    function handle_output(out){
    var res = out.content.data["text/plain"];
    $("div#tweet_text").html(res);
    }

    $("input#capture").keypress(function(e){
    if(e.which == 48){
    set_label(0);
    }
    else if(e.which == 49){
    set_label(1);
    }
    else{
    alert("This is an invalid input");
    }
    $("input#capture").val("");
    });
    })
    </script>
    """))


    Why the code kernel.execute("labels.append("+ JSON.stringify(label) +")"); cannot be executed with the error:TypeError: kernel.execute is not a function. Please help me

    I have tried to change IPython to Jupyter, but there is an error:ReferenceError: Jupyter is not defined. Consider the jupyter version issue

    Continue reading...

Compartilhe esta Página