我正在使用python创建一个破折号(按图)应用程序。当我尝试在浏览器中运行它时,我能够使其在本地运行,但我的csv数据均未真正呈现(我只是在后台看到空白轴。我唯一感兴趣的是第一个一个(不明飞行物地图)。其他我没有为其创建回调的文件。
这是我的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from flask_caching import Cache
from csv import DictReader
from toolz import compose, pluck, groupby, valmap, first, unique, get,
countby
import datetime as dt
import numpy as np
import pandas as pd
import os
listpluck = compose(list, pluck)
listfilter = compose(list, filter)
listmap = compose(list, map)
listunique = compose(list, unique)
TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
df = pd.read_csv('ufo_sightings.csv')
# Datetime helpers.
def sighting_year(sighting):
return dt.datetime.strptime(sighting[df['date_time']],
TIMESTAMP_FORMAT).year
def sighting_dow(sighting):
return dt.datetime.strptime(sighting[df['date_time']],
TIMESTAMP_FORMAT)\
.strftime("%a")
def ufo_map(sightings):
classifications = groupby('shape', sightings)
return {
"data": [
{
"type": "scattermapbox",
"lat": listpluck("city_latitude", class_sightings),
"lon": listpluck("city_longitude", class_sightings),
"text": listpluck("summary", class_sightings),
"mode": "markers",
"name": shape,
"marker": {
"size": 3,
"opacity": 1.0
}
}
for shape, class_sightings in classifications.items()
],
"layout": {
"autosize": True,
"hovermode": "closest",
"mapbox": {
"accesstoken": os.environ.get("MAPBOX_KEY"),
"bearing": 0,
"center": {
"lat": 40,
"lon": -98.5
},
"pitch": 0,
"zoom": 2,
"style": "outdoors"
}
}
}
reader = DictReader(df)
BFRO_LOCATION_DATA = [
line for line in reader ]
app = dash.Dash()
server = app.server
server.secret_key = os.environ.get("SECRET_KEY", "secret")
app.title = "UFO Sightings"
cache = Cache(app.server, config={"CACHE_TYPE": "simple"})
@cache.memoize(10)
def filter_sightings(filter_text):
return listfilter(
lambda x: filter_text.lower() in x['summary'].lower(),
BFRO_LOCATION_DATA
)
app.layout = html.Div([
# Row: Title
html.Div([
# Column: Title
html.Div([
html.H1("UFO Sightings", className="text-center")
], className="col-md-12")
], className="row"),
# Row: Filter + References
html.Div([
# Column: Filter
html.Div([
html.P([
html.B("Filter the titles: "),
dcc.Input(
placeholder="Try 'saw'",
id="ufo-text-filter",
value="")
]),
], className="col-md-6"),
# Column: References.
html.Div([
html.P([
"Data pulled from ",
html.A("nuforc.org", href="http://www.nuforc.org/"),
". Grab it at ",
html.A("data.world",
href="https://data.world/timothyrenner/ufo-sightings"),
"."
], style={"text-align": "right"})
], className="col-md-6")
], className="row"),
# Row: Map + Bar Chart
html.Div([
# Column: Map
html.Div([
dcc.Graph(id="ufo-map")
], className="col-md-8"),
# Column: Bar Chart
html.Div([
dcc.Graph(id="ufo-dow")
], className="col-md-4")
], className="row"),
# Row: Line Chart + Donut Chart
html.Div([
# Column: Line Chart
html.Div([
dcc.Graph(id="ufo-by-year")
], className="col-md-8"),
# Column: Donut Chart
html.Div([
dcc.Graph(id="ufo-class")
], className="col-md-4")
], className="row"),
# Row: Footer
html.Div([
html.Hr(),
html.P([
"A Deplorable Snowflake Production",
html.A("blank",
href="https://blank.org"),
])
], className="row",
style={
"textAlign": "center",
"color": "Gray"
})
], className="container-fluid")
@app.callback(
Output('ufo-map', 'figure'),
[
Input('ufo-text-filter', 'value')
]
)
def filter_ufo_map(filter_text):
return ufo_map(filter_sightings(filter_text))
if __name__ == "__main__":
app.run_server(debug=True)
这是完整的追溯:
Traceback (most recent call last):
File "/lib/python3.6/site-packages/flask/app.py", line 2309, in
__call__
return self.wsgi_app(environ, start_response)
File "/lib/python3.6/site-packages/flask/app.py", line 2295, in
wsgi_app
response = self.handle_exception(e)
File "/python3.6/site-packages/flask/app.py", line 1741, in
handle_exception
reraise(exc_type, exc_value, tb)
File "/lib/python3.6/site-packages/flask/_compat.py", line 35, in
reraise
raise value
File "/lib/python3.6/site-packages/flask/app.py", line 2292, in
wsgi_app
response = self.full_dispatch_request()
File "/lib/python3.6/site-packages/flask/app.py", line 1815, in
full_dispatch_request
rv = self.handle_user_exception(e)
File "/lib/python3.6/site-packages/flask/app.py", line 1718, in
handle_user_exception
reraise(exc_type, exc_value, tb)
File "/lib/python3.6/site-packages/flask/_compat.py", line 35, in
reraise
raise value
File "/lib/python3.6/site-packages/flask/app.py", line 1813, in
full_dispatch_request
rv = self.dispatch_request()
File "/lib/python3.6/site-packages/flask/app.py", line 1799, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/lib/python3.6/site-packages/dash/dash.py", line 558, in dispatch
return self.callback_map[target_id]['callback'](*args)
File "/lib/python3.6/site-packages/dash/dash.py", line 515, in
add_context
output_value = func(*args, **kwargs)
File "/alien.py", line 220, in filter_ufo_map
return ufo_map(filter_sightings(filter_text))
File "/alien.py", line 47, in ufo_map
classifications = groupby('shape', sightings)
File "/lib/python3.6/site-packages/toolz/itertoolz.py", line 93, in
groupby
d[key(item)](item)
KeyError: 'shape'
在这里的任何帮助将不胜感激。谢谢。