Search code examples
pythonjupyter-notebookpymongo

NameError - How to fix?


I am working on a project and am having issues with a NameError. When I ran the code the previous day, my data table was output with no issues. However, I got the NameError the next day even though I didn't make any changes.

The NameError I am getting refers to this line of code:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-fd215c7f5d79> in <module>
     66                  ]),
     67 
---> 68     dash_table.DataTable(
     69         id='datatable-interactivity',
     70         columns=[

NameError: name 'dash_table' is not defined

The issue I am having is with the following code:

import dash
import dash_leaflet as dl
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import dash_table as dt
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

from CRUD import AnimalShelter


username = "ZaneBrown"
password = "Zoe"
shelter = AnimalShelter(username, password)
 
df = pd.DataFrame.from_records(shelter.read({}))



app = JupyterDash('SimpleExample')


image_filename = 'Grazioso-Salvare-Logo.png' 
encoded_image = base64.b64encode(open(image_filename, 'rb').read())


app.layout = html.Div([
    html.Div(id='hidden-div', style={'display':'none'}),
    html.Center(html.B(html.H1('Zane Brown SNHU CS-340 Dashboard'))),
    html.Img(src='data:image/png;base64,{}'.format(encoded_image.decode())),
    html.Hr(),
    

    
    html.Div(className='row',
            style={'display': 'flex'},
                 children=[
                     html.Button(id='submit-button-one', n_clicks=0, children='Cats'),
                     html.Button(id='submit-button-two', n_clicks=0, children='Dogs')
                 ]),
    
    dash_table.DataTable(
        id='datatable-interactivity',
        columns=[
            {"name": i, "id": i, "deletable": False, "selectable": True} for i in df.columns
        ],
        data=df.to_dict('records'),

        editable=False,
        filter_action="native",
        sort_action="native",
        sort_mode="multi",
        column_selectable=False,
        row_selectable="single",
        row_deletable=False,
        selected_columns=[],
        selected_rows=[0],
        page_action="native",
        page_current= 0,
        page_size= 10,

    ),
     html.Br(),
     html.Hr(),

    html.Div(className='row',
         style={'display' : 'flex'},
             children=[
        html.Div(
            id='graph-id',
            className='col s12 m6',

            ),
        html.Div(
            id='map-id',
            className='col s12 m6',
            )
        ])
])


@app.callback(Output('datatable-interactivity',"data"),
              [Input('submit-button-one', 'n_clicks'),Input('submit-button-two', 'n_clicks'),
              ])
def on_click(bt1,bt2):
              #start case
              if (int(bt1) == 0 and int(bt2) == 0):
                  df = pd.DataFrame.from_records(shelter.readAll({}))
              # use higher number of button clicks to determine filter type
              elif (int(bt1) > int(bt2)):
                    df = pd.DataFrame(list(shelter.readAll({"animal_type":"Cat"})))
      
              elif (int(bt2) > int(bt1)):
                df = pd.DataFrame(list(shelter.readAll({"animal_type":"Dog"})))
              
              return df.to_dict('records')
app

I appreciate the help!


Solution

  • You are using import dash_table as dt. Then you can't use dash_table, you have to use dt instead.