-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
215 lines (179 loc) · 8.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import sqlite3
import pandas as pd
from datetime import datetime as dt
import jobs
import os
from geopy import distance
from geopy import Point
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True
# Query that returns a new data frame from an SQL argument
def query(arg="SELECT * FROM jobs"):
databaseConnection = sqlite3.connect('jobs.db')
try:
newDataFrame = pd.read_sql_query(arg, databaseConnection)
except pd.io.sql.DatabaseError:
newDataFrame = None
databaseConnection.close()
return newDataFrame
def query_by_distance(dataFrame, radius):
queryList = dataFrame.to_dict('records')
newList = []
bridgeWaterLatitude = 41.9904
bridgeWaterLongitude = 70.9751
for row in queryList:
latitude = row['geo_latitude']
longitude = row['geo_longitude']
if latitude is None or longitude is None:
continue
if is_inside_radius(bridgeWaterLatitude, bridgeWaterLongitude,
abs(float(latitude)), abs(float(longitude)), radius):
newList.append(row)
return pd.DataFrame(newList)
# Function that returns more details about a job and/or more jobs if they exist at the same coordinate.
def return_more_job_information(lon, lat):
totalString = []
if lon is None and lat is None:
temp_data_frame = query("SELECT * FROM JOBS WHERE JOBS.GEO_LONGITUDE IS NULL AND JOBS.GEO_LATITUDE IS NULL;")
else:
temp_data_frame = query("SELECT * FROM JOBS WHERE JOBS.GEO_LONGITUDE = '{}' "
"AND JOBS.GEO_LATITUDE = '{}'".format(lon, lat))
titles = temp_data_frame["Title"]
descriptions = temp_data_frame['Description']
datesPosted = temp_data_frame['Created_at']
for index in range(len(titles)):
totalString += [index + 1, ") ", "Date posted: ", datesPosted[index], html.Br(), "Title: ", titles[index],
html.Br(),
"Description: ", descriptions[index], html.Br(), html.Br()]
return totalString
# Function that returns a new map box figure each there a new query is posed.
def return_figure(data_frame):
figure = go.Figure(data=go.Scattermapbox(
lon=data_frame['geo_longitude'],
lat=data_frame['geo_latitude'],
text="Job Description: " + data_frame['Description'].str.slice(0, 75) + "</br>"
+ data_frame['Description'].str.slice(75, 150) + "..."
+ "</br>Location: " + data_frame['Location']
+ "</br>Job Title: " + data_frame['Title']
+ "</br>Company: " + data_frame['Company']
+ "</br>Click on data and scroll down for more details and jobs at this location!",
mode='markers',
))
figure.update_layout(
mapbox_style="open-street-map",
margin={'l': 0, 't': 0, 'b': 0, 'r': 0},
mapbox={
'center': {'lon': 10, 'lat': 10},
'zoom': 1})
return figure
def is_inside_radius(latitude1, longitude1, latitude2, longitude2, radius):
if latitude1 is not None and longitude1 is not None and latitude2 is not None and longitude2 is not None:
point1 = Point(latitude1, longitude1)
point2 = Point(latitude2, longitude2)
result = distance.distance(point1, point2).miles
if result <= radius:
return True
return False
# Just a simple function that checks if a table named jobs already exists. If it exists,
# then a prompt is given out whether or not to run jobs.main()
def check_if_exists():
found = False
if query("SELECT geo_longitude, geo_latitude FROM JOBS") is not None:
found = True
else:
if os.path.exists("jobs.db"):
os.remove("jobs.db")
answer = ''
if found:
while answer.lower() not in ['y', 'n']:
answer = input("Database found before initialization. Would you like to skip finding jobs? Y or N>>")
if found and answer == 'n' or not found:
print("Initializing data from jobs...")
jobs.main()
# Callback function for filtering data.
@app.callback(
Output(component_id='map', component_property='figure'),
[Input(component_id='locationInput', component_property='value'),
Input(component_id='locationByDistanceInput', component_property='value'),
Input(component_id='techInput', component_property='value'),
Input(component_id='companyInput', component_property='value'),
Input(component_id='datePick', component_property='start_date'),
Input(component_id='datePick', component_property='end_date')])
def update_output_div(map_input, location_by_distance_input, tech_input, company_input, start_date, end_date):
temporaryDF = query("SELECT * FROM jobs WHERE UPPER(jobs.description) "
"LIKE '%{}%' AND UPPER(jobs.location) LIKE '%{}%' AND "
"julianday('{}') <= julianday(jobs.Created_At) AND "
"julianday('{}') >= julianday(jobs.Created_At) AND "
"UPPER(jobs.Company) LIKE '%{}%';".format(
tech_input.upper(), map_input.upper(), start_date, end_date, company_input.upper()))
if location_by_distance_input is not None and location_by_distance_input != '':
secondTempDF = query_by_distance(temporaryDF, float(location_by_distance_input))
if secondTempDF.empty:
# Running an extremely weird and unefficent code to retrieve an empty dataframe with columns.
temporaryDF = query("SELECT * FROM JOBS WHERE JOBS.LOCATION = 'IJADIJAIDJA';")
else:
temporaryDF = secondTempDF
return return_figure(temporaryDF)
# Callback function for retrieving more information on jobs.
@app.callback(
Output('additionalInfo', 'children'),
[Input('map', 'clickData'),
Input('my-button', 'n_clicks')])
def display_click_data(click_data, n_clicks):
global buttonClicks
if n_clicks is not None and n_clicks > buttonClicks:
buttonClicks = n_clicks
return return_more_job_information(None, None)
else:
if click_data is not None:
moreData = click_data['points'][0]
lon, lat = moreData['lon'], moreData['lat']
return return_more_job_information(lon, lat)
else:
return "No data selected. Please make sure to click on a data point to view more jobs in that area."
if __name__ == '__main__':
app.title = "Jobs Map Filter"
check_if_exists()
buttonClicks = 0
app.layout = html.Div([
html.H5(
children='Welcome to the job seeking tool.',
style={
'textAlign': 'center',
}
),
html.Label(children='Filter Location - eg. Boston, MA'),
dcc.Input(id='locationInput', value='', type='text'),
html.Label(children='Filter Distance in Miles from Bridgewater'),
dcc.Input(id='locationByDistanceInput', value='', type='number'),
html.Label(children='Filter Job Technology - eg. Python'),
dcc.Input(id='techInput', value='', type='text'),
html.Label(children='Filter Job Company - eg. Facebook'),
dcc.Input(id='companyInput', value='', type='text'),
html.Br(),
html.Button('Click to display Remote Jobs Information Below', id='my-button'),
html.Label('Filter jobs by date.'),
dcc.DatePickerRange(
id="datePick",
start_date=dt(2020, 1, 1),
end_date=dt.today(),
display_format='MMM Do, YYYY',
start_date_placeholder_text='Start Period',
end_date_placeholder_text="End Period",
),
html.Br(),
dcc.Graph(
id="map",
figure=return_figure(query()),
),
html.H4(children="More Details or Remote Jobs"),
html.Label(id="additionalInfo", children="No data selected. Please make sure to click on a "
"data point to view more jobs in that area.")
])
app.run_server(debug=True, use_reloader=False)