为什么python不能从python函数中调用Javascript()? [英] Why cannot python call Javascript() from within a python function?

查看:136
本文介绍了为什么python不能从python函数中调用Javascript()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用从iPython Notebook下载CSV 建议的代码动态构建javascript代码,并在从jupyter笔记本调用时使用python中的Javascript()将其传递给浏览器。代码效果很好。如果我在python函数中嵌入相同的代码并从同一个jupyter笔记本调用python函数,则python中的调用Javascript()不再有效。如何使可重用功能正常工作?



我在Windows 10上运行的Chrome版本73.0.3683.103(官方构建)(64位)上尝试此操作。如果有抱歉的话已经回答了。我已经搜索了SO和google。



这有效..

 来自IPython.display导入Javascript 
js_download =
var csv ='%s';

var filename ='results.csv';
var blob = new Blob([csv],{type:'text / csv; charset = utf-8;'});
if(navigator.msSaveBlob){// IE 10+
navigator.msSaveBlob( blob,filename);
} else {
var link = document.createElement(a);
if(link.download!== undefined){// feature detection
//支持HTML5下载属性的浏览器
var url = URL.createObjectURL(blob);
link.setAttribute(href,url);
link.setAttribute(download, filename);
link.style.visibility ='hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(链接);
}
}
%data_in_dataframes.to_csv(index = False).replace('\ r','\ \'''。replace('\ n','\\ n')。replace(',\')
Javascript(js_download)

这不起作用,它无法执行Javascript(js_download),但它也没有给出我在jupyter中看到的任何错误笔记本也不是浏览器中的java控制台。就好像它从未执行Javascript(js_download)。

 来自IPython.display import Javascript 
def js_download_csv(df_download,s_filename ='results.csv'):
js_download =
var csv ='%s';
var filename ='%s';
var blob = new Blob([csv],{type:'text / csv; charset = utf-8;'});
if(navigator.msSaveBlob){// IE 10+
navigator.msSaveBlob(blob,filename);
} else {
var link = document.createElement(a);
if(link.download!== undefined){/ / feature detection
//支持HTML5下载属性的浏览器
var url = URL.createObjectURL(blob);
link.setAttribute(href,url);
link。 setAttribute(download,filename);
link.style.visibility ='hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
%(df_download.to_csv(index = False).replace('\ r','\\\\')。replace('\ n','\\ n')。replace(',\'),s_filename)
Javascript(js_download)
返回无
js_download_csv(df_download,s_filename )

如果我遗漏任何明显的东西,我会道歉。我找不到任何有关任何信息的错误或日志发生了什么。



欢迎任何建议。

解决方案

我有找到了一个部分答案,因为虽然我不知道为什么会出现这个问题,但我已经找到了如何克服它。在 https://medium.com/@tomgrek/reactive-python-javascript-communication-in-jupyter-notebook-e2a879e25906 我们在文章中看到以下问题:



一个大问题:某个地方需要返回Javascript作为其输出,否则它不会在笔记本中执行。



因此,如果我们将代码更改为以下(即返回Javascript调用),它可以工作。

 来自IPython.display import Javascript 
def js_download_csv(df_download,s_filename = 'results.csv'):
js_download =
var csv ='%s';
var filename ='%s';
var blob = new Blob( [csv],{type:'text / csv; charset = utf-8;'});
if(navigator.msSaveBlob){// IE 10+
navigator.msSaveBlob(blob,filename) ;
} else {
var link = document.createElement(a);
if(link.download!== undefined){// feature detection
//浏览器支持HTML5下载属性
var url = URL.createObjectURL(blob);
link.setAttribute(href,url);
link.setAttribute(download,filename);
link.style.visibility ='hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
%(df_download.to_csv(index = False).replace('\ r','\\\\')。replace('\ n',' \\ n')。replace(',\'),s_filename)
返回Javascript(js_download)
js_download_csv(df_download,s_filename)


I used the code suggested from Download CSV from an iPython Notebook to dynamically build the javascript code and pass it to the browser using Javascript() in python when called from a jupyter notebook. Code works great. If I embed the same code in a python function and call the python function from the same jupyter notebook, the call Javascript() in python no longer works. How can I make the reusable function work?

I am trying this on Chrome Version 73.0.3683.103 (Official Build) (64-bit) running on Windows 10. Apologies if already answered. I have scoured SO and google.

This works..

from IPython.display import Javascript
js_download = """
var csv = '%s';

var filename = 'results.csv';
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
    navigator.msSaveBlob(blob, filename);
} else {
    var link = document.createElement("a");
    if (link.download !== undefined) { // feature detection
        // Browsers that support HTML5 download attribute
        var url = URL.createObjectURL(blob);
        link.setAttribute("href", url);
        link.setAttribute("download", filename);
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
}
""" % data_in_dataframes.to_csv(index=False).replace('\r','\\r').replace('\n','\\n').replace("'","\'")
Javascript(js_download)

This does not work, it fails to execute Javascript(js_download), but neither does it give any error that I can see in the jupyter notebook nor the java console in the browser. It is as if it never executed Javascript(js_download).

from IPython.display import Javascript
def js_download_csv(df_download, s_filename='results.csv'):
    js_download =   """
    var csv = '%s';
    var filename = '%s';
    var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
    if (navigator.msSaveBlob) { // IE 10+
        navigator.msSaveBlob(blob, filename);
    } else {
        var link = document.createElement("a");
        if (link.download !== undefined) { // feature detection
            // Browsers that support HTML5 download attribute
            var url = URL.createObjectURL(blob);
            link.setAttribute("href", url);
            link.setAttribute("download", filename);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
    """ % (df_download.to_csv(index=False).replace('\r','\\r').replace('\n','\\n').replace("'","\'"), s_filename)
    Javascript(js_download)
    return None
js_download_csv(df_download, s_filename)

Apologies if I have left anything obvious out. I can find no errors or logs with any information regarding what is happening.

Any suggestions welcome.

解决方案

I have found a partial answer, in that while I do not why this problem occurs, I have found how to overcome it. In https://medium.com/@tomgrek/reactive-python-javascript-communication-in-jupyter-notebook-e2a879e25906 we see in the article the following gotcha:

A big gotcha: something somewhere needs to return Javascript as its output, otherwise it doesn’t get executed in the notebook.

So, if we change the code to the following (i.e. return the Javascript call), it works.

from IPython.display import Javascript
def js_download_csv(df_download, s_filename='results.csv'):
    js_download =   """
    var csv = '%s';
    var filename = '%s';
    var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
    if (navigator.msSaveBlob) { // IE 10+
        navigator.msSaveBlob(blob, filename);
    } else {
        var link = document.createElement("a");
        if (link.download !== undefined) { // feature detection
            // Browsers that support HTML5 download attribute
            var url = URL.createObjectURL(blob);
            link.setAttribute("href", url);
            link.setAttribute("download", filename);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    }
    """ % (df_download.to_csv(index=False).replace('\r','\\r').replace('\n','\\n').replace("'","\'"), s_filename)
    return Javascript(js_download)
js_download_csv(df_download, s_filename)

这篇关于为什么python不能从python函数中调用Javascript()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆