1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.