仪表板应用程序未呈现

2024-09-27 21:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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) 

以下是完整的回溯:

^{pr2}$

如能在这里提供任何帮助,我们将不胜感激。谢谢您。在


Tags: textimportdivappmaphtmlcolumncol

热门问题