Black Lives Matter. Please consider donating to Black Girls Code today.

Python Dashboard error py.dashboard_ops.upload

Hi, I’m trying to replicate the dashboard example here: https://plot.ly/python/create-online-dashboard/

I got 2 errors:

  1. using the “fileId_from_url” function I get the raw_fileId as empty so that when I try to access it
    raw_fileId = re.findall("~[A-z]+/[0-9]+", url)[0][1: ]
    I get the error
    IndexError: list index out of range
  2. If I manually change “/” to “:” in the urls, I can go on. When I try to create the dashboard with the line:
    py.dashboard_ops.upload(my_dboard, ‘My First Dashboard with Pythonvv’)
    I get the error:
    raise exceptions.PlotlyRequestError(message, status_code, content)
  3. What if I want to try to run it offline? If I import “import plotly.offline as pyo” I can’t use “pyo.dashboard_ops.upload” anymore.

Thanks in advance :slight_smile:
I add the code below

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')