Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON < MONGODB int(bt2)): df = pd.DataFrame(list(shelter.read({animal__type:Cat}))) df =pd.DataFrame(list(shelter.read({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

PYTHON< MONGODB

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 that I have the wrong file type when I try to load my data into the dataframe using json.dumps. I am sure that this is just one error in a long list of errors as I really have no idea what I am doing. I am pretty good at java but when it comes to this stuff I have a pretty hard time. So, if anyone could help me along with this assignment and maybe just point out what is wrong with my code or if I need to add anything it would be greatly appreciated. I am pretty desperate for help.

from jupyter_plotly_dash import JupyterDash as jd

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 bson 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(list(dumps(shelter.readAll({})))) ## Having trouble here

#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.Center(html.B.H6('Jacqueline Woods')), #Unique Handle 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(), html.Div(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)), 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.read({"animal__type":"Cat"}))) df =pd.DataFrame(list(shelter.read({"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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

PYTHON Answered: 1 week ago

Answered: 1 week ago