Skip to content

Pull request #245 to fix issue #17116 mentioned in jupyter-notebook repo gives too much output #246

@kiliansinger

Description

@kiliansinger

I reported microsoft/vscode-jupyter#17116 for the vscode-jupyter plugin stating that the element variable is not exposed in javascript magic environment. It was fixed in this pull request.
#245

And indeed output now appears but additionally some source code is output.

Following example:

%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42 //type annotations are not available in javascript you need to use typescript
           //that is translated into javascript by a compiler
console.log("I am "+age+" old\n")

output:

 (function(){
                    let gotToUserScript = false;
                    try {
                        // Required by JS code in Jupyter notebook renderers such as ipyvega.
                        // We're not fully supporting ipyvega yet, but this ensures the scripts will not fall over and will work with minimal effort on our part.
                        const context = {
                            outputs: [{
                                metadata: {}, data: {}
                            }],
                        };
                        // Required by JS code in Jupyter notebook renderers again, even scenepic (Microsoft Python widget) uses this.
                        const ele = document.getElementById("78a4cefb-af83-488c-8ab5-2152c172af23");
                        (function (element){
                            gotToUserScript = true;
                            element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42 //type annotations are not available in javascript you need to use typescript
           //that is translated into javascript by a compiler
console.log("I am "+age+" old\n")

                        }).call(context, ele);
                    } catch (ex) {
                        console.error('VS Code Renderer failed to render output', ex);
                        if (gotToUserScript) {
                            throw ex;
                        } else {
                            // Something went wrong in our script that was generated by us.
                            element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

let age=42 //type annotations are not available in javascript you need to use typescript
           //that is translated into javascript by a compiler
console.log("I am "+age+" old\n")

                        }
                    }
                })();I am 42 old

Expected output:

I am 42 old

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions