python 有没有禁用网络的方法,在Python中禁用Plotly以任何形式与网络通信

我想我已经为此提出了解决方案.首先,你需要下载开源Plotly.js

file.然后我有一个函数,写在下面,将生成python图中的javascript并引用你的本地副本plotly-latest.min.js.见下文:

import sys

import os

from plotly import session, tools, utils

import uuid

import json

def get_plotlyjs():

path = os.path.join('offline', 'plotly.min.js')

plotlyjs = resource_string('plotly', path).decode('utf-8')

return plotlyjs

def js_convert(figure_or_data,outfilename, show_link=False, link_text='Export to plot.ly',

validate=True):

figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)

width = figure.get('layout', {}).get('width', '100%')

height = figure.get('layout', {}).get('height', 525)

try:

float(width)

except (ValueError, TypeError):

pass

else:

width = str(width) + 'px'

try:

float(width)

except (ValueError, TypeError):

pass

else:

width = str(width) + 'px'

plotdivid = uuid.uuid4()

jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)

jlayout = json.dumps(figure.get('layout', {}), cls=utils.PlotlyJSONEncoder)

config = {}

config['showLink'] = show_link

config['linkText'] = link_text

config["displaylogo"]=False

config["modeBarButtonsToRemove"]= ['sendDataToCloud']

jconfig = json.dumps(config)

plotly_platform_url = session.get_session_config().get('plotly_domain',

'https://plot.ly')

if (plotly_platform_url != 'https://plot.ly' and

link_text == 'Export to plot.ly'):

link_domain = plotly_platform_url\

.replace('https://', '')\

.replace('http://', '')

link_text = link_text.replace('plot.ly', link_domain)

script = '\n'.join([

'Plotly.plot("{id}", {data}, {layout}, {config}).then(function() {{',

' $(".{id}.loading").remove();',

'}})'

]).format(id=plotdivid,

data=jdata,

layout=jlayout,

config=jconfig)

html="""

Drawing...

class="plotly-graph-div">

{script}

""".format(id=plotdivid, script=script,

height=height, width=width)

#html = html.replace('\n', '')

with open(outfilename, 'wb') as out:

#out.write(r'')

out.write(r'')

for line in html.split('\n'):

out.write(line)

out.close()

print ('JS Conversion Complete')

消除所有链接的关键线是:

config['showLink'] = show_link #False

....

config["modeBarButtonsToRemove"]= ['sendDataToCloud']

您可以这样调用函数来获取一个静态HTML文件,该文件引用了您的本地开源库的副本:

fig = {

"data": [{

"x": [1, 2, 3],

"y": [4, 2, 5]

}],

"layout": {

"title": "hello world"

}

}

js_convert(fig, 'test.html')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值