【问题标题】:Plotly (offline) for Python click eventPython点击事件的Plotly(离线)
【发布时间】:2017-05-27 22:41:59
【问题描述】:

是否可以将点击事件添加到 Plotly 散点图中(Python 中的离线模式)?

例如,我想在被点击时改变一组散点的形状。

到目前为止我尝试了什么

我从网站上阅读其他问题(没有明确答案)的理解是,我可能必须生成 html,然后在事后通过输入 javascript 代码对其进行编辑?所以我可以写一个javascript函数,把它保存到my_js.js,然后从html链接到它?

【问题讨论】:

    标签: javascript python plotly scatter


    【解决方案1】:

    我一直在 plotly 中做一些离线情节的工作,并且遇到了同样的挑战。

    这是我想出的一个组合,我证明它可以作为其他人的灵感。

    一些限制:

    • 假设您在单个 html 文件中为单个绘图提供离线输出。
    • 假设您的 on 事件与事件处理程序的名称相同。
    • 需要美汤 4。
    • 假设您已经安装了 lxml。
    • 使用 Plotly 2.2.2 开发

    代码片段:

    import bs4
    
    def add_custom_plotly_events(
        filename, 
        events = {
            "plotly_click": "function plotly_click(data) { console.log(data); }",
            "plotly_hover": "function plotly_hover(data) { console.log(data); }"
        },
        prettify_html = True
    ):
    
        # what the value we're looking for the javascript
        find_string = "Plotly.newPlot"
    
        # stop if we find this value
        stop_string = "then(function(myPlot)"
    
        def locate_newplot_script_tag(soup):    
            scripts = soup.find_all('script')
            script_tag = soup.find_all(string=re.compile(find_string))
    
            if len(script_tag) == 0:
                raise ValueError("Couldn't locate the newPlot javascript in {}".format(filename))
            elif len(script_tag) > 1:
                raise ValueError("Located multiple newPlot javascript in {}".format(filename))
    
            if script_tag[0].find(stop_string) > -1:
                raise ValueError("Already updated javascript, it contains:", stop_string)
    
            return script_tag[0]
    
        def split_javascript_lines(new_plot_script_tag):
            return new_plot_script_tag.string.split(";")
    
        def find_newplot_creation_line(javascript_lines):
            for index, line in enumerate(javascript_lines):
                if line.find(find_string) > -1:
                    return index, line
            raise ValueError("Missing new plot creation in javascript, couldn't find:", find_string)
    
        def join_javascript_lines(javascript_lines):
            # join the lines with javascript line terminator ;    
            return ";".join(javascript_lines)
    
        def register_on_events(events):
            on_events_registration = []
            for function_name in events:
                on_events_registration.append("myPlot.on('{}', {})".format(
                    function_name, function_name
                ))
            return on_events_registration
    
        # load the file
        with open(filename) as inf:
            txt = inf.read()
            soup = bs4.BeautifulSoup(txt, "lxml")
    
        new_plot_script_tag = locate_newplot_script_tag(soup)
    
        javascript_lines = split_javascript_lines(new_plot_script_tag)
    
        line_index, line_text = find_newplot_creation_line(javascript_lines)    
    
        on_events_registration = register_on_events(events)
    
        # replace whitespace characters with actual whitespace
        # using + to concat the strings as {} in format
        # causes fun times with {} as the brackets in js
        # could possibly overcome this with in ES6 arrows and such
        line_text = line_text + ".then(function(myPlot) { " + join_javascript_lines(on_events_registration) +"  })".replace('\n', ' ').replace('\r', '')
    
        # now add the function bodies we've register in the on handles
        for function_name in events:
            javascript_lines.append(events[function_name])
    
        # update the specific line
        javascript_lines[line_index] = line_text
    
        # update the text of the script tag
        new_plot_script_tag.string.replace_with(join_javascript_lines(javascript_lines))
    
        # save the file again
        with open(filename, "w") as outf:
            # tbh the pretty out is still ugly af
            if prettify_html:
                for line in soup.prettify(formatter = None):
                    outf.write(str(line))
            else:
                outf.write(str(soup))
    

    【讨论】:

    • 这个功能怎么用?如果你能展示一个(基本的)例子,那就太好了!
    • 作为报告生成过程的一部分,我生成了一些散点图(状态聚合,以及区域的状态聚合下方)。由于防火墙的乐趣,我需要它们成为离线情节。我使用 onclick 在州级聚合之间移动以深入了解区域级聚合。
    【解决方案2】:

    根据 Plotly 的 community site 上的 Click events in python offline mode?,至少截至 2015 年 12 月,这不受支持。

    如果您喜欢冒险,那篇文章确实包含一些关于如何自己实现此功能的提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多