

@option('-w', '--workspace', action='store', default=None, help='use the provided name as workspace filename')
def line_jigsaw(self, language, workspace=None):
    '\n        %jigsaw LANGUAGE - show visual code editor/generator\n\n        This line magic will allow visual code editing or generation.\n\n        Examples:\n            %jigsaw Processing\n            %jigsaw Python\n            %jigsaw Processing --workspace workspace1\n        '
    if (workspace is None):
        workspace = ('jigsaw-workspace-' + ''.join((random.SystemRandom().choice((string.ascii_uppercase + string.digits)) for i in range(6))))
    workspace_filename = (workspace + '.xml')
    html_text = download((('https://calysto.github.io/jigsaw/' + language) + '.html'))
    html_filename = (workspace + '.html')
    html_text = html_text.replace('MYWORKSPACENAME', fp)
    with open(html_filename, 'w') as fp:
        fp.write(html_text)
    script = '\n    if (document.jigsaw_register_workspace === undefined) {\n\n        document.jigsaw_workspaces = {};\n\n        document.jigsaw_register_workspace = function(workspace_filename, workspace, xml_init) {\n            workspace.element = document.element;\n            document.jigsaw_workspaces[workspace_filename] = workspace;\n\n            try {\n                $([window.parent.IPython.events]).on(\'notebook_saved.Notebook\', function() { \n                    try {\n                        document.jigsaw_save_workspace(workspace_filename); \n                    } catch(err) {\n                        // ignore failure, might not exist\n                    }\n                });\n            } catch (err) {\n                // rendering for display\n            }\n            \n            var xml = document.jigsaw_loadXMLDoc(workspace_filename);\n            if (xml === null) {\n                xml = xml_init;\n                if (xml === null) {\n                    xml = Blockly.Xml.textToDom(\'<xml id="workspace"></xml>\');\n                }\n            } else {\n                xml = xml.children[0]; // document\n            }\n            Blockly.Xml.domToWorkspace(workspace, xml);\n        };\n\n        document.jigsaw_handle_output = function(workspace_filename, out) {\n            var workspace = document.jigsaw_workspaces[workspace_filename];\n            //var output_area = workspace.element.output_area;\n            var cell_index = document.jigsaw_get_cell(workspace.element);\n            var cell = IPython.notebook.get_cell(cell_index);\n            var res = null;\n            var data = null;\n            document.cell = cell;\n            document.out = out;\n            if (out.msg_type == "stream") {\n                res = out.content.text;\n                //document.getElementById(\'code_output\').value += res.toString();\n            } else if (out.msg_type === "pyout") {\n                // if output is a python object\n                res = out.content.data["text/plain"];\n                //document.getElementById(\'code_output\').value += res.toString(); \n            } else if (out.msg_type == "pyerr") {\n                // if output is a python error\n                res = out.content.data["text/plain"];\n                //document.getElementById(\'code_output\').value += res.toString();\n            } else if (out.msg_type == "execute_result") {\n                var str = out.content.data["text/plain"];\n                res = str;\n                if (res.indexOf("u") == 0)\n                    res = res.substring(2, res.length - 1) + "\\n";\n                if (res) {\n                    //document.getElementById(\'code_output\').value += res.toString();\n                }\n            } else if (out.msg_type == "error") {\n                res = out.content.ename + ": " + out.content.evalue + "\\n";\n                // FIXME: out.traceback is Array of terminal color-coded [-codes\n            } else {\n                // if output is something we haven\'t thought of\n                res = out.toString();\n                //document.getElementById(\'code_output\').value += res.toString();\n            }\n            if (res) {\n                cell.output_area.append_output({output_type: "stream", text: res.toString(), name: "output"});\n            }\n        };\n        \n        document.jigsaw_generate = function(workspace_filename, language, insert_code) {\n            var workspace = document.jigsaw_workspaces[workspace_filename];\n            var callbacks = { \'iopub\' : {\'output\' : function(out) { document.jigsaw_handle_output(workspace_filename, out); }}};\n            var code = Blockly[language].workspaceToCode(workspace);\n            if (insert_code == 1) {\n                var cell_index = document.jigsaw_get_cell(workspace.element);\n                var cell = IPython.notebook.insert_cell_at_index(0, cell_index + 1);\n                cell.set_text(code);\n            } else {\n                window.parent.IPython.notebook.kernel.execute(code,\n                                                              callbacks,\n                                                              {silent: false});\n            }\n        };\n        \n        document.jigsaw_save_workspace = function(workspace_filename) {\n            var workspace = document.jigsaw_workspaces[workspace_filename];\n            var xml = Blockly.Xml.workspaceToDom(workspace);\n            document.xml = xml;\n            if (xml !== undefined) {\n                console.log(xml);\n                //xml.style = "display: none";\n                //xml.id = "workspace";\n                var xml_text = Blockly.Xml.domToText(xml)\n                IPython.notebook.kernel.execute(\'%%file \' + workspace_filename + \'\\n\' + xml_text);\n            }\n        };\n        \n        document.jigsaw_loadXMLDoc = function(filename) {\n            var xhttp = new XMLHttpRequest();\n            xhttp.open("GET", filename, false);\n            xhttp.send();\n            return xhttp.responseXML;\n        };\n    }\n\n    document.jigsaw_get_cell = function (element) {\n        // FIXME: brittle and ugly:\n        var mydiv = element[0].parentNode.parentNode.parentNode.parentNode;\n        var cells = IPython.notebook.get_cells();\n        for (var i = 0; i < cells.length; i++) {\n            if (mydiv === cells[i].element[0]) {\n                return i;\n            }\n        }\n        return null;\n    };\n\n    document.jigsaw_clear_output = function (workspace_filename) {\n        var workspace = document.jigsaw_workspaces[workspace_filename];\n        var cell_index = document.jigsaw_get_cell(workspace.element);\n        var cell = IPython.notebook.get_cell(cell_index);\n        // FIXME: brittle and ugly:\n        cell.element[0].children[2].children[1].children[2].children[1].children[0].innerHTML = ""\n        cell.output_area.outputs[2].text = ""\n    };\n\n    try {\n        document.element = element;\n    } catch (err) {\n        // rendering\n    }\n'
    script = script.replace('MYWORKSPACENAME', workspace_filename)
    iframe = ('<iframe src="%s" width="100%%" height="350" style="resize: both; overflow: auto;"></iframe>' % html_filename)
    self.kernel.Display(Javascript(script))
    self.kernel.Display(HTML(iframe))

dataset/ETHPy150Open Calysto/metakernel/metakernel/magics/jigsaw_magic.py JigsawMagic.line_jigsaw/VarMisuse@126/282 `workspace_filename`->`fp`