Getting [removed] and
tags from Plotly using Python

后端 未结 3 1966
失恋的感觉
失恋的感觉 2020-12-30 10:55

I was wondering if anyone knew a good way (preferably a built in method, but I\'m open to writing my own of course) to get the

3条回答
  •  太阳男子
    2020-12-30 11:14

    Apologies for the necro-answer really wanted to add a comment to Fermin Silva left behind (https://stackoverflow.com/a/38033016/2805700) - but long standing lurker reputation prevents me.

    Anyhow I had a similar need and encoutered an issue with plotly 2.2.2

    plotly.offline.plot(data, include_plotlyjs=False, output_type='div')
    

    The include_plotlyjs parameter was being ignored when outputting to a div. Based on the comments above, I found a workaround. Basically let plotly plot to file, which does respect the include_plotlyjs parameter. Load into beautiful soup and inject the link to the latest plotly.js on the cdn.

    import plotly
    import bs4
    
    # return as html fragment
    # the include_plotlyjs argument seems to be 
    # ignored as it's included regardless when outputting to div
    # found an open issue on here - https://github.com/plotly/plotly.py/issues/1043
    plotly.offline.plot(
        plot_output, 
        filename = filename,
        config = plot_config,
        include_plotlyjs = False,
        auto_open = False,
    )
    
    # load the file
    with open(filename) as inf:
        txt = inf.read()
        soup = bs4.BeautifulSoup(txt)
    
    # add in the latest plot-ly js as per https://stackoverflow.com/a/38033016/2805700
    js_src = soup.new_tag("script", src="https://cdn.plot.ly/plotly-latest.min.js")
    # insert it into the document
    soup.head.insert(0, js_src)
    
    # save the file again
    with open(filename, "w") as outf:
        outf.write(str(soup))
    

    Cheers

提交回复
热议问题