【问题标题】:NameError - How to fix?NameError - 如何修复?
【发布时间】:2020-12-11 23:06:19
【问题描述】:

我正在处理一个项目,但遇到了 NameError 问题。当我前一天运行代码时,我的数据表输出没有问题。但是,即使我没有进行任何更改,第二天我也收到了 NameError。

我得到的 NameError 指的是这行代码:

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

我遇到的问题是以下代码:

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

感谢您的帮助!

【问题讨论】:

  • 您导入了 dash_table as dt 使用 dt 代替或仅使用 import dash_table
  • 我明白了!非常感谢!

标签: python jupyter-notebook pymongo


【解决方案1】:

您正在使用import dash_table as dt。那你不能用dash_table,你必须用dt代替。

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 2019-11-22
  • 2020-11-28
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多