Plotly Python仪表板错误py.仪表板操作。上传

2024-04-27 04:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图复制仪表板示例here

我有两个错误:

1)使用“fileId_from_url”函数,我将raw_fileId设为空,这样当我试图访问它时

raw_fileId = re.findall("~[A-z]+/[0-9]+", url)[0][1: ]

我知道错误了

^{pr2}$

2)如果我手动将url中的“/”更改为“:”,我可以继续。当我尝试使用以下行创建仪表板时:

py.dashboard_ops.upload(my_dboard, ‘My First Dashboard with Pythonvv’)

我得到了一个错误:

raise exceptions.PlotlyRequestError(message, status_code, content)

3)如果我想离线运行该怎么办?如果我导入“import plotly.offline as pyo”我就不能再使用“pyo.dashboard_ops.upload”。在

提前谢谢。在

我在下面添加代码

import plotly.plotly as py
py.sign_in(username='myname', api_key='mypass')
#%%
import plotly.dashboard_objs as dashboard
import IPython.display
from IPython.display import Image

my_dboard = dashboard.Dashboard()
my_dboard.get_preview()
#%%
import plotly.graph_objs as go
import numpy as np

colorscale = [[0, '#FAEE1C'], [0.33, '#F3558E'], [0.66, '#9C1DE7'], [1, '#581B98']]
trace1 = go.Scatter(
    y = np.random.randn(500),
    mode='markers',
    marker=dict(
        size='16',
        color = np.random.randn(500),
        colorscale=colorscale,
        showscale=True
    )
)
data = [trace1]
url_1 = py.plot(data, filename='scatter-for-dashboard', auto_open=False)
py.iplot(data, filename='scatter-for-dashboard')
#%%

x0 = np.random.randn(50)
x1 = np.random.randn(50) + 2
x2 = np.random.randn(50) + 4
x3 = np.random.randn(50) + 6

colors = ['#FAEE1C', '#F3558E', '#9C1DE7', '#581B98']

trace0 = go.Box(x=x0, marker={'color': colors[0]})
trace1 = go.Box(x=x1, marker={'color': colors[1]})
trace2 = go.Box(x=x2, marker={'color': colors[2]})
trace3 = go.Box(x=x3, marker={'color': colors[3]})
data = [trace0, trace1, trace2, trace3]

url_2 = py.plot(data, filename='box-plots-for-dashboard', auto_open=False)
py.iplot(data, filename='box-plots-for-dashboard')
#%%
import re

def fileId_from_url(url):
    """Return fileId from a url."""
    raw_fileId = re.findall("~[A-z]+/[0-9]+", url)[0][1: ]
    return raw_fileId.replace('/', ':')

def sharekey_from_url(url):
    """Return the sharekey from a url."""
    if 'share_key=' not in url:
        return "This url is not 'sercret'. It does not have a secret key."
    return url[url.find('share_key=') + len('share_key='):]

fileId_1 = fileId_from_url(url_1)
fileId_2 = fileId_from_url(url_2)

box_a = {
    'type': 'box',
    'boxType': 'plot',
    'fileId': fileId_1,
    'title': 'scatter-for-dashboard'
}

text_for_box = ""

box_b = {
    'type': 'box',
    'boxType': 'text',
    'text': text_for_box,
    'title': 'Markdown Options for Text Box'
}

box_c = {
    'type': 'box',
    'boxType': 'plot',
    'fileId': fileId_2,
    'title': 'box-for-dashboard',
    'shareKey': sharekey_from_url(url_2)
}

my_dboard.insert(box_a)
my_dboard.insert(box_b, 'above', 1)

#%%
my_dboard.get_box(1)
my_dboard.get_box(1)['title'] = 'a new title'
my_dboard.get_box(1)
my_dboard.insert(box_a, 'below', 2)
my_dboard['settings']['logoUrl'] = 'https://images.plot.ly/language-icons/api-home/python-logo.png'
my_dboard['settings']['links'] = []
my_dboard['settings']['links'].append({'title': 'Link to Plotly', 'url': 'https://plot.ly/'})
my_dboard['settings']['links'].append({'title': 'Link to Python Website', 'url': 'https://www.python.org/'})
my_dboard['settings']['foregroundColor'] = '#000000'
my_dboard['settings']['backgroundColor'] = '#adcaea'
my_dboard['settings']['headerForegroundColor'] = '#ffffff'
my_dboard['settings']['headerBackgroundColor'] = '#D232C8'
my_dboard['settings']['boxBackgroundColor'] = '#ffffff'
my_dboard['settings']['boxBorderColor'] = '#000000'
my_dboard['settings']['boxHeaderBackgroundColor'] = '#ffffff'

stacked_dboard = dashboard.Dashboard()
text_box = {
    'type': 'box',
    'boxType': 'text',
    'text': 'empty space' 
}
for _ in range(5):
    stacked_dboard.insert(text_box, 'below', 1)

 #%%   
stacked_dboard['layout']['size'] = 3000
my_dboard['layout']['first']['first'] = text_for_box
py.dashboard_ops.upload(my_dboard, 'My First Dashboard with Pythonvv')

Tags: textfrompyimportboxurlgofor