Question
I am very new to python and mongodb. I know we are using the dash core framework as well (see imports) I am trying to
I am very new to python and mongodb. I know we are using the dash core framework as well (see imports) I am trying to get this code to get the mongo collection printed to a table and also use that collection to make a location map and a pie chart. I am getting an error stating "File "
from jupyter_plotly_dash import JupyterDash
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 from dash.dependencies import Input, Output from bson.json_util import dumps,loads
import numpy as np import pandas as pd import matplotlib.pyplot as plt from pymongo import MongoClient from AAC import AnimalShelter
########################### # Data Manipulation / Model ########################### # FIX ME update with your username and password and CRUD Python module name
username = "aacuser" password = "Melvin1234!" shelter = AnimalShelter() shelter._init_(username,password)
# class read method must support return of cursor object and accept projection json input df = pd.DataFrame.from_records(loads(shelter.readAll())
#dff = pd.DataFrame.from_records(loads.shelter.readAll())
######################### # Dashboard Layout / View #########################
app = JupyterDash('SimpleExample')
app.layout = html.Div([ html.Div(id='hidden-div', style={'display':'none'}), html.Center(html.B(html.H1('SNHU CS-340 Dashboard'))), 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') ]), html.Br(), 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, page_action='none'), html.Br(), html.Div( dcc.Graph( id ='graph_id', figure=shelter.readAll(), title="Outcome_Type" )), html.Br(), html.Div( id='map-id', className='col s12 m6', title="Location" )])
############################################# # Interaction Between Components / Controller ############################################# #This callback will highlight a row on the data table when the user selects it @app.callback( Output('datatable-interactivity',"data"), [Input('submit-button-one','n_clicks'), Input('submit-button-two','n_clicks') ] ) def on_click(bt1, bt2): if(int(bt1) ==0 and int(bt2) ==0): df =pd.DataFrame.from_records(shelter.readAll({})) elif (int(bt1) > int(bt2)): df = pd.DataFrame(list(shelter.readAll({"animal__type":"Cat"}))) df =pd.DataFrame(list(shelter.readAll({"animal_type":"Dog"}))) return df.to_dict('records') @app.callback( Output('datatable-interactivity', 'style_data_conditional'), [Input('datatable-interactivity', 'selected_columns')] ) def update_styles(selected_columns): return [{ 'if': { 'column_id': i }, 'background_color': '#D2F3FF' } for i in selected_columns]
#@app.callback( # Output('graph_id','figure'), # [Input('datatable-interactivity',dff['outcome_type'])]) #def generateGraph(allData): # pieChart = px.pie(dff['outcome_type'] #,hole =0.3,) # return pieChart @app.callback( Output('graph-id','figure'), [Input('datatable-interactivity',"derived_virtual_data")]) def updateGraph(allData): dff= pd.DataFrame(allData) pieChart = px.pie( data_frame=dff, names=dff['outcome_type'], hole = .3, ) return pieChart
@app.callback( Output('map-id', "children"), [Input('datatable-interactivity', 'derived_viewport_data'), Input('datatable-interactivity', 'derived_virtual_selected_rows') ]) def update_map(viewData): #FIXME Add in the code for your geolocation chart dff = pd.DataFrame.from_dict(viewData) dff = df if viewData is None else pd.DataFrame(viewData) selected_animal = None if not derived_virtual_selected_rows: slected_animal = dff.iloc[0] else: slected_animal = dff.iloc[derived_vertual_selected_rows[0]] latitude = selected_animal[12] longitude =selected_animal[13] breed = selected_animal[3] name = selected_animal[0] # Austin TX is at [30.75,-97.48] 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=[latitude,longitude], children=[ dl.Tooltip(breed), dl.Popup([ html.H1("Animal Name"), html.P(name) ]) ]) ]) ]
app
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started