Skip to content

Commit

Permalink
Uploaded Project 2 Artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisQGeorge authored Feb 23, 2021
0 parents commit 0216595
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 0 deletions.
298 changes: 298 additions & 0 deletions GraziosoSalvareShelterDogFinder.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
" <iframe src=\"/app/endpoints/c6152b55dda14cbabdaea9f38ea44c5d/\" width=800 height=600 frameborder=\"0\"></iframe>\n",
" <hr/><a href=\"/app/endpoints/c6152b55dda14cbabdaea9f38ea44c5d/\" target=\"_new\">Open in new window</a> for /app/endpoints/c6152b55dda14cbabdaea9f38ea44c5d/\n",
"</div>"
],
"text/plain": [
"<jupyter_plotly_dash.dash_wrapper.JupyterDash at 0x7f0a8f762e80>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from jupyter_plotly_dash import JupyterDash\n",
"\n",
"import dash\n",
"import dash_leaflet as dl\n",
"import dash_core_components as dcc\n",
"import dash_html_components as html\n",
"import plotly.express as px\n",
"import dash_table as dt\n",
"from dash.dependencies import Input, Output, State\n",
"\n",
"import os\n",
"import numpy as np\n",
"import pandas as pd\n",
"from pymongo import MongoClient\n",
"from bson.json_util import dumps\n",
"import base64\n",
"\n",
"\n",
"from crud import CRUD\n",
"\n",
"\n",
"\n",
"###########################\n",
"# Data Manipulation / Model\n",
"###########################\n",
"port = \"51915\"\n",
"database = \"AAC\"\n",
"collection = \"animals\"\n",
"username = \"aacuser\"\n",
"password = \"8rBn8A8CZ\"\n",
"shelter = CRUD(port, database, collection, username, password)\n",
"\n",
"\n",
"# class read method must support return of cursor object \n",
"df = pd.DataFrame.from_records(shelter.read({'animal_type':'Dog'}))\n",
"\n",
"\n",
"\n",
"#########################\n",
"# Dashboard Layout / View\n",
"#########################\n",
"app = JupyterDash('SimpleExample')\n",
"\n",
"image_filename = 'GraziosoSalvareLogo.png' # replace with your own image\n",
"encoded_image = base64.b64encode(open(image_filename, 'rb').read())\n",
"\n",
"\n",
"app.layout = html.Div([\n",
"# html.Div(id='hidden-div', style={'display':'none'}),\n",
" html.Div([\n",
" html.Div([\n",
" html.Div([\n",
" html.A([\n",
" html.Img(\n",
" src='data:image/png;base64,{}'.format(encoded_image.decode()),\n",
" style = {'height':'100px', 'width':'100px'})],\n",
" href = 'https://www.snhu.edu/')],\n",
" style = {'float': 'center', 'display':'inline-block'})], \n",
" style = {'width':'100px', 'display': 'inline-block', 'overflow':'auto'}),\n",
" \n",
" html.Div([\n",
" html.Div([\n",
" html.B(html.H1('SNHU CS-340 Dashboard'))],\n",
" style = {'float': 'center', 'display': 'inline-block'})],\n",
" style = {'width':'calc(100%-100px)', 'display': 'inline-block', 'overflow':'auto'}\n",
" )], \n",
" style = {'width':'100%', 'display':'block', 'overflow':'auto'}\n",
" \n",
" \n",
" ),\n",
" html.Footer([html.H5(children = \"Created by Christopher George 2021\")],\n",
" \n",
" style = {'background-color':'#45433d', 'color':'white', 'height':'15px'}\n",
" ),\n",
" html.Hr(),\n",
" html.Div(\n",
" \n",
" dcc.RadioItems(\n",
" options = [\n",
" {'label':\"Water Rescue\", 'value':'1'},\n",
" {'label':\"Wilderness Rescue\", 'value':'2'},\n",
" {'label':\"Individual Tracking\", 'value':'3'},\n",
" {'label':\"reset\", 'value':'0'},\n",
" ], \n",
" id = 'filter-type')\n",
" ),\n",
" html.Hr(),\n",
" dt.DataTable(id='datatable-id', style_table = {'height':'300px', 'width':'100vw', 'overflow':'auto'}),\n",
" html.Br(),\n",
" html.Hr(),\n",
"#This sets up the dashboard so that your chart and your geolocation chart are side-by-side\n",
" html.Div(className='row',\n",
" style={'display' : 'flex'},\n",
" children=[\n",
" html.Div(\n",
" id='graph-id',\n",
" className='col s12 m6',\n",
"\n",
" ),\n",
" html.Div(\n",
" id='map-id',\n",
" className='col s12 m6',\n",
" )\n",
" ]),\n",
"html.Footer([html.H5(children = \"Created by Christopher George 2021\")],\n",
" \n",
" style = {'background-color':'#45433d', 'color':'white', 'height':'15px'}\n",
")\n",
"])\n",
"\n",
"#############################################\n",
"# Interaction Between Components / Controller\n",
"#############################################\n",
"\n",
"\n",
"\n",
" \n",
"@app.callback([Output('datatable-id','data'),\n",
" Output('datatable-id','columns')],\n",
" [Input('filter-type', 'value')])\n",
"def update_dashboard(filter_type):\n",
" \n",
" if(filter_type == '1'):\n",
" df = pd.DataFrame.from_records(shelter.read({\n",
" 'animal_type':'Dog',\n",
" 'breed':{'$in':[\n",
" 'Labrador Retriever Mix',\n",
" 'Chesapeake Bay Retriever',\n",
" 'Newfoundland']},\n",
" 'sex_upon_outcome':'Intact Female',\n",
" 'age_upon_outcome_in_weeks':{'$gt':26,'$lt':156}\n",
" }))\n",
" elif(filter_type == '2'):\n",
" df = pd.DataFrame.from_records(shelter.read({\n",
" 'animal_type':'Dog',\n",
" 'breed':{'$in':['German Shepherd',\n",
" 'Alaskan Malamute',\n",
" 'Old English Sheepdog',\n",
" 'Siberian Husky',\n",
" 'Rottweiler']},\n",
" 'sex_upon_outcome':'Intact Male',\n",
" 'age_upon_outcome_in_weeks':{'$gt':26,'$lt':156}\n",
" }))\n",
" elif(filter_type == '3'):\n",
" df = pd.DataFrame.from_records(shelter.read({\n",
" 'animal_type':'Dog',\n",
" 'breed':{'$in':['Doberman Pinscher',\n",
" 'GermanShepherd',\n",
" 'Golden Retriever',\n",
" 'Bloodhound',\n",
" 'Rottweiler']},\n",
" 'sex_upon_outcome':'Intact Male',\n",
" 'age_upon_outcome_in_weeks':{'$gt':20,'$lt':300}\n",
" }))\n",
" else:\n",
" df = pd.DataFrame.from_records(shelter.read({'animal_type':'Dog'})) \n",
" \n",
" columns=[{\"name\": i, \"id\": i, \"deletable\": False, \"selectable\": True} for i in df.columns]\n",
" data=df.to_dict('records')\n",
" \n",
" \n",
" return (data,columns)\n",
"\n",
"\n",
"\n",
"\n",
"@app.callback(\n",
" Output('datatable-id', 'style_data_conditional'),\n",
" [Input('datatable-id', 'selected_columns')]\n",
")\n",
"def update_styles(selected_columns):\n",
" return [{\n",
" 'if': { 'column_id': i },\n",
" 'background_color': '#D2F3FF'\n",
" } for i in selected_columns]\n",
"\n",
"@app.callback(\n",
" Output('graph-id', \"children\"),\n",
" [Input('datatable-id', \"derived_viewport_data\")])\n",
"def update_graphs(viewData):\n",
" data = viewData\n",
"\n",
" for i in data:\n",
" i.update({'count': 1})\n",
" \n",
" return [\n",
" dcc.Graph( \n",
" figure = px.pie(data, values = \"count\", names = \"breed\" )\n",
" ) \n",
" ]\n",
"\n",
"@app.callback(\n",
" Output('map-id', \"children\"),\n",
" [Input('datatable-id', \"derived_viewport_data\")])\n",
"\n",
"def update_map(viewData):\n",
" data = viewData\n",
" markerList = [dl.TileLayer(id=\"base-layer-id\")]\n",
" x = 0\n",
" for i in data:\n",
" markerList.append(\n",
" dl.Marker(position=[i.get('location_lat'), i.get('location_long')], children=[\n",
" dl.Tooltip(i.get('breed')),\n",
" dl.Popup([\n",
" html.H1(\"Animal Name\"),\n",
" html.P(i.get('name'))\n",
" ])\n",
" ])\n",
" )\n",
" if x == 0:\n",
" center_cords = [i.get('location_lat'), i.get('location_long')]\n",
" x = 1\n",
"\n",
" \n",
" \n",
" # Austin TX is at [30.75,-97.48]\n",
" return [\n",
" dl.Map(style={'width': '1000px', 'height': '500px'},\n",
" center=center_cords,\n",
" zoom=9, \n",
" children=markerList\n",
" )\n",
" ]\n",
"\n",
"app\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added README.docx
Binary file not shown.
87 changes: 87 additions & 0 deletions crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from pymongo import MongoClient
from bson.objectid import ObjectId
import pprint

class CRUD(object):
""" CRUD operations for Animal collection in MongoDB """

def __init__(self, port, db, collection, username, password):
# Initializing the MongoClient. This helps to
# access the MongoDB databases and collections.
uri = "mongodb://"+ username+":"+password+"@localhost:"+port+"/admin"
client = MongoClient(uri)
self.client = client
self.database = self.client[db]
self.collection = self.database[collection]

# Complete this create method to implement the C in CRUD.
def create(self, data):
if data is not None:
if isinstance(data, dict):
self.collection.insert_one(data) # data should be dictionary
return True

else:
raise Exception("data type error: data parameter is not a dictionary")
else:
raise Exception("Nothing to save, because data parameter is empty")
return False

# Create method to implement the R in CRUD.
def read(self, query):
if query is not None:
if isinstance(query, dict):
i = 0
docHolder = []
for document in self.collection.find(query):
i = 1
docHolder.append(document)
if i != 0:
return self.collection.find(query,{"_id":False})

else:
print("query not found in collection")
else:
print("data type error: data parameter is not a dictionary")
raise Exception("data type error: data parameter is not a dictionary")
else:
print("Nothing to find, because given query is empty")
raise Exception("Nothing to find, because given query is empty")

#Update method to implement U in CRUD
def update(self, query, data):
if data is not None:
if isinstance(data, dict):
if query is not None:
if isinstance(query, dict):
results = self.collection.update_many(query, {'$set':data})

return results.raw_result
else:
print("data type error: query parameter is not a dictionary")
raise Exception("data type error: query parameter is not a dictionary")
else:
print("error: given query is empty")
raise Exception("given query is empty")
else:
print("data type error: data parameter is not a dictionary")
raise Exception("data type error: data parameter is not a dictionary")
else:
print("error: given data is empty")
raise Exception("given data is empty")

#delete method to implement D in CRUD
def delete(self, query):
if query is not None:
if isinstance(query, dict):
results = self.collection.delete_one(query)
return results.raw_result
else:
print("data type error: data parameter is not a dictionary")
raise Exception("data type error: data parameter is not a dictionary")
else:
print("Nothing to find, because given query is empty")
raise Exception("Nothing to find, because given query is empty")



0 comments on commit 0216595

Please sign in to comment.