-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefault.py
110 lines (90 loc) · 2.45 KB
/
default.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
import urllib2
import auth
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input
import plotly.graph_objs as go
import simplejson as simplejs
app = dash.Dash(__name__)
key = auth.key
with open('token.txt','r') as tk:
access = tk.read()
def current_location(name):
default = 'http://ip-api.com/json'
open_d = urllib2.urlopen(default)
default_js = json.load(open_d)
lat = default_js['lat']
lon = default_js['lon']
return lat, lon
def show_climate(name):
lat, lon = current_location(name)
url = 'http://api.openweathermap.org/data/2.5/weather?lat='+str(lat)+'&lon='+str(lon)+'&appid='+key
open_url = urllib2.urlopen(url)
open_w = json.load(open_url)
clouds = open_w['clouds']
cloudy = clouds.values()[0]
weather = open_w['weather'][0]['description']
temp = open_w['main']['temp']
temp_c = temp - 273
temp_c = round(temp_c, 2)
temp_f = temp_c * 9/5 + 32
temp_f = round(temp_f, 2)
ws = open_w['wind']['speed']
return weather, temp_c, ws
app.layout = html.Div([
html.H3('Current location tracker'),
dcc.Input(id='input', value=''),
html.Div(id='output-graph'),
dcc.Graph(id='graph')
])
@app.callback(
Output('graph', 'figure'),
[Input('input', 'value')]
)
def location_tracker(name):
lat, lon = current_location(name)
weather_type, celsius, wind_speed = show_climate(name)
lat = str(lat)
lon = str(lon)
data = go.Data([
go.Scattermapbox(
lat=[lat],
lon=[lon],
mode='markers',
marker=go.Marker(
size=11
),
text=[str(name).title() + ' ' + str(weather_type) + ' ' + str(celsius) + ' C ' + str(wind_speed) + ' mph'],
#hoverlabel=dict(bgcolor='rgba(188, 20, 26, 0.5)'),
hoverinfo='text'
)
])
layout = go.Layout(
#width=600,
height=620,
autosize=True,
showlegend=False,
hovermode='closest',
mapbox=dict(
accesstoken=access,
bearing=1,
center=dict(
lat=int(lat.split('.')[0]),
lon=int(lon.split('.')[0])
),
pitch=0,
zoom=6,
style='dark'
),
)
return {'data' : data, 'layout' : layout}
external_css = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
for css in external_css:
app.css.append_css({'external_url' : css})
external_js = ['https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js']
for js in external_js:
app.scripts.append_script({'external_url' : js})
if __name__ == '__main__':
app.run_server(debug=True)