[英]Change plotly html plot title in browser tab
我使用plotly
通過將數據保存為html
小部件來可視化我的數據,並在瀏覽器中打開它,因為我發現本機查看器非常慢。
離線代碼 PLOT
library(plotly)
libraray(htmlwidgets)
offlinePlot <- function(pname, fname, browse = T){
saveWidget(as_widget(x = pname), file = fname)
if(browse){
browseURL(fname)
}
}
offlinePlot(pname = sampleplot, fname = sampleplot.html, browse = T)
一般來說,我在瀏覽器中同時打開了幾個圖,它們渲染得很好,但是所有圖在瀏覽器選項卡中都有相同的標題,有沒有辦法改變它並將選項卡標題設置為自定義?
有一個類似鏈接到此頁面的帖子: https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js但我不太明白。
這是一個處理從 python 設置瀏覽器標題的 hack。 首先創建一個名為titlerenderer.py
的文件並包含以下代碼:
from plotly.io._base_renderers import BrowserRenderer, open_html_in_browser
from plotly.io._renderers import renderers
class TitleBrowserRenderer(BrowserRenderer):
def __init__(
self,
config=None,
auto_play=False,
using=None,
new=0,
autoraise=True,
post_script=None,
animation_opts=None,
):
super().__init__(
config, auto_play, using, new, autoraise, post_script, animation_opts
)
browser_tab_title = "Undefined"
def render(self, fig_dict):
from plotly.io import to_html
html = (
"""
<title>
"""
+ self.browser_tab_title
+ """
</title>
"""
+ to_html(
fig_dict,
config=self.config,
auto_play=self.auto_play,
include_plotlyjs=True,
include_mathjax="cdn",
post_script=self.post_script,
full_html=True,
animation_opts=self.animation_opts,
default_width="100%",
default_height="100%",
validate=False,
)
)
open_html_in_browser(html, self.using, self.new, self.autoraise)
renderers["titleBrowser"] = TitleBrowserRenderer()
然后在您的 plotly.express 代碼中,使用:
import titlereader # Needed to hook in the renderer
# Create your figure
fig.show(renderer="titleBrowser", browser_tab_title="MY TITLE")
不能保證這是健壯的,但它適用於我的用例!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.