'Can't get graphical map to display; IndexError: index 4 is out of bounds for axis 0 with size 0
So I'm working on an interactive dashboard using Jupyter Plotly Dash, where I am importing a database from MongoDB. I am trying to get a geographical map to display underneath my data table, however it does not appear. I took a look at the terminal running Jupyter Notebook, and I did take note of an error; IndexError: index 4 is out of bounds for axis 0 with size 0. I am not entirely sure if this error is relevant to my problem or not. I did some research on this type of error, and from my understanding, it would usually occur if there is no data within the python list. I'm especially curious as to why it is saying there is an axis 0 with size 0, as I expected my code to have a size of 14. I guess what is confusing me is the fact that my dataset seems to import itself correctly, as the data table fills itself with data as expected. I'm still learning this, so any advice in the right direction would be appreciated.
from jupyter_plotly_dash import JupyterDash
import dash
import dash_leaflet as dl
from dash import html, dcc, dash_table
import plotly.express as px
from dash.dependencies import Input, Output, State
import base64
import os
import numpy as np
import pandas as pd
from pymongo import MongoClient
from bson.json_util import dumps
import matplotlib.pyplot as plt
from pyth_script import CustomDashboard
###########################
# Data Manipulation / Model
###########################
# User authentication
username = "myuser"
password = "teambds"
pyths = CustomDashboard(username, password)
# Convert PyMongo cursor into Pandas DataFrame
df = pd.DataFrame(list(pyths.read_all({})))
#########################
# Dashboard Layout / View
#########################
app = JupyterDash('ANM Database')
# Declare the application interfaces
app.layout = html.Div([
html.Div(id='hidden-div', style={'display':'none'}),
html.Center(html.B(html.H1('Custom Dashboard'))),
html.Hr(),
dash_table.DataTable(
id='datatable-id',
columns=[
{"name": i, "id": i, "deletable": False, "selectable": True} for i in df.columns
],
data=df.to_dict('records'),
editable=False,
filter_action="native",
row_selectable="single",
selected_columns=[],
page_size = 15
),
html.Br(),
html.Hr(),
html.Div(
id='map-id',
className='col s12 m6',
)
])
#############################################
# Interaction Between Components / Controller
#############################################
@app.callback(
Output('datatable-id', 'style_data_conditional'),
[Input('datatable-id', 'selected_columns')]
)
def update_styles(selected_columns):
return [{
'if': { 'column_id': i },
'background_color': '#D2F3FF'
} for i in selected_columns]
@app.callback(
Output('map-id', "children"),
[Input('datatable-id', "derived_viewport_data")])
def update_map(viewData):
dff = pd.DataFrame.from_dict(viewData)
return [
dl.Map(style={'width': '1000px', 'height': '500px'}, center=[30.75,-97.48], zoom=10, children=[
dl.TileLayer(id="base-layer-id"),
# Marker with tool tip and popup
dl.Marker(position=[30.75,-97.48], children=[
dl.Tooltip(dff.iloc[0,4]),
dl.Popup([
html.H1("Name"),
html.P(dff.iloc[1,9])
])
])
])
]
app
The compiled result; https://cdn.discordapp.com/attachments/684573403625160704/964591129465085962/Screen_Shot_2022-04-15_at_2.19.43_PM.png https://cdn.discordapp.com/attachments/684573403625160704/964591113384128612/Screen_Shot_2022-04-15_at_2.19.52_PM.png
The full error trace;
[I 10:14:29.662 NotebookApp] Kernel started: 20a09017-8a0c-4fa0-9297-6f1d6f433faf, name: python3
[IPKernelApp] ERROR | Exception in comm_msg for 64c788f60e65402e9c8bec7feb95a8a3
Traceback (most recent call last):
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/ipykernel/comm/manager.py", line 109, in comm_msg
comm.handle_msg(msg)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/ipykernel/comm/comm.py", line 161, in handle_msg
self._msg_callback(msg)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/jupyter_plotly_dash/nbkernel.py", line 47, in callback
response, mimetype = app.process_view(stem, args, app_path)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/jupyter_plotly_dash/dash_wrapper.py", line 152, in process_view
resp, rmt = func(args, app_path, view_name_parts)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/jupyter_plotly_dash/dash_wrapper.py", line 193, in rv__dash_update_component
resp = dapp.dispatch_with_args(args, argMap)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/django_plotly_dash/dash_wrapper.py", line 698, in dispatch_with_args
res = callback(*args, **{k: v for k, v in argMap.items() if k in parameters_to_inject})
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/dash/_callback.py", line 151, in add_context
output_value = func(*func_args, **func_kwargs) # %% callback invoked %%
File "/var/folders/n6/3w8lx4ds5cn4kybq464lmrww0000gn/T/ipykernel_82408/3817460729.py", line 88, in update_map
dl.Tooltip(dff.iloc[0,4]),
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexing.py", line 960, in __getitem__
return self.obj._get_value(*key, takeable=self._takeable)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py", line 3612, in _get_value
series = self._ixs(col, axis=1)
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/pandas/core/frame.py", line 3439, in _ixs
label = self.columns[i]
File "/Users/jake/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 5039, in __getitem__
return getitem(key)
IndexError: index 4 is out of bounds for axis 0 with size 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
