-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py
More file actions
239 lines (221 loc) · 8.94 KB
/
Copy pathapp.py
File metadata and controls
239 lines (221 loc) · 8.94 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Dash Related Imports
from dash import Dash, html, dcc, Output, Input
import plotly.express as px
# PySpark and databricks-connect related imports.
from databricks.connect.session import DatabricksSession as SparkSession
from databricks.sdk.core import Config
from pyspark.sql.functions import col
from pyspark.sql.types import StringType
import pyspark.sql.functions as F
# Generic Imports
import json
# Load the JS and CSS for Syntax Highlighting
external_scripts = ["https://cdn.tailwindcss.com"]
external_stylesheets = [
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css"
]
app = Dash(
__name__,
external_scripts=external_scripts,
external_stylesheets=external_stylesheets,
)
app.layout = html.Div(
className="container mx-auto max-w-4xl mt-8 bg-white p-3",
children=[
html.H1(
className="text-2xl mb-4 text-red-500 font-bold",
children="NYC Taxi Cockpit: Plotly x Databricks Demo",
),
html.P(
"""This is a sample application to show-case how easy it is to get started with
Databricks Connect and build interactive Python applications. The dataset used for
this application is the standard Databricks samples dataset. All you need to get
started is a Databricks cluster and this simple application.""",
),
html.Div(
[
html.H2(
"NYC Taxi analysis (data processing on Databricks)",
className="text-xl mt-4 mb-4 text-red-500 font-bold",
),
html.P(
"""The below visualization uses a heatmap display based on geo-coordinates for
either the pickup or dropoff dimension and a second dimension is used for coloring. """
),
html.Div(
[
html.Div("Dimension 1"),
html.Div(
dcc.Dropdown(
["pickup_zip", "dropoff_zip"],
"dropoff_zip",
id="Map-dropdown-zip",
className="form-input",
)
),
],
className="columns-2",
),
html.Div(
[
html.Div("Dimension 2"),
html.Div(
dcc.Dropdown(
[
"avg_trip_duration",
"cout_trips",
"avg_trip_distance",
],
"avg_trip_duration",
id="Map-dropdown-agg",
)
),
],
className="columns-2",
),
dcc.Graph(id="NYC-zip-map-analysis"),
html.H2(
"Example Code", className="text-xl mt-4 mb-4 text-red-500 font-bold"
),
html.Div(
[
html.Pre(
"""
@app.callback(
Output("NYC-zip-map-analysis", "figure"),
[Input('Map-dropdown-zip', 'value'),
Input('Map-dropdown-agg', 'value')]
)
def update_output(zip_plot, column_map_show):
df = spark.read.table("samples.nyctaxi.trips")
df = df.withColumn('DurationInMin', F.round(
(F.unix_timestamp("tpep_dropoff_datetime") - F.unix_timestamp("tpep_pickup_datetime")) / 60, 2))
df = df.where(df.DurationInMin < 200)
df = df.withColumn("day_of_week", F.date_format("tpep_pickup_datetime", 'E'))
df_map = (df.groupBy(col(zip_plot).alias('zip_code')).agg(
F.avg('DurationInMin').alias('avg_trip_duration'),
F.count('DurationInMin').alias('cout_trips'),
F.avg('trip_distance').alias('avg_trip_distance'))
)
df_map_show = df_map.toPandas()
df_map_show.head()
nycmap = json.load(open("nyc-zip-code-tabulation-areas-polygons.geojson"))
return px.choropleth_mapbox(df_map_show,
geojson=nycmap,
locations="zip_code",
featureidkey="properties.postalCode",
color=column_map_show,
color_continuous_scale="viridis",
mapbox_style="carto-positron",
zoom=9, center={"lat": 40.7, "lon": -73.9},
opacity=0.7,
hover_name="zip_code"
)
""",
className="text-xs border-solid border-2 border-slate-400 rounded bg-slate-200 p-4 overflow-scroll",
id="code-hl",
)
]
),
]
),
html.Div(
[
html.H2(
"Trips by pickup zip(data processing on Databricks)",
className="text-xl mt-4 mb-4 text-red-500 font-bold",
),
html.P(),
html.Div(
className="columns-2 gap-8",
children=[
html.Div("Count >="),
dcc.Input(
id="count-input",
value="10",
type="number",
className="border border-solid form-input border-slate-400",
),
],
),
html.Div(dcc.Graph(id="postcode-trip-count")),
html.H2("Example Code", className="text-xl mt-4 mb-4"),
html.Div(
[
html.Pre(
"""
@app.callback(
Output("postcode-trip-count", "figure"),
Input("count-input", "value")
)
def update_trip_count(greaterThan):
df = spark.read.table("samples.nyctaxi.trips")
df = df.withColumn("pickup_zip",
col("pickup_zip").cast(StringType())).withColumn("dropoff_zip",
col("dropoff_zip").cast(
StringType()))
df = df.groupBy("pickup_zip", "dropoff_zip").count()
df = df.filter(col("count") >= int(greaterThan))
return px.scatter(df.toPandas(), x="pickup_zip", y="dropoff_zip", size="count")
""",
className="text-xs border-solid border-2 border-slate-400 rounded bg-slate-200 p-4 overflow-scroll",
)
]
),
]
),
],
)
config = Config(profile="PROFILE", cluster_id="CLUSTER_ID")
spark = SparkSession.builder.sdkConfig(config).getOrCreate()
@app.callback(Output("postcode-trip-count", "figure"), Input("count-input", "value"))
def update_trip_count(greaterThan):
df = spark.read.table("samples.nyctaxi.trips")
df = df.withColumn("pickup_zip", col("pickup_zip").cast(StringType())).withColumn(
"dropoff_zip", col("dropoff_zip").cast(StringType())
)
df = df.groupBy("pickup_zip", "dropoff_zip").count()
df = df.filter(col("count") >= int(greaterThan))
return px.scatter(df.toPandas(), x="pickup_zip", y="dropoff_zip", size="count")
@app.callback(
Output("NYC-zip-map-analysis", "figure"),
[Input("Map-dropdown-zip", "value"), Input("Map-dropdown-agg", "value")],
)
def update_output(zip_plot, column_map_show):
df = spark.read.table("samples.nyctaxi.trips")
df = df.withColumn(
"DurationInMin",
F.round(
(
F.unix_timestamp("tpep_dropoff_datetime")
- F.unix_timestamp("tpep_pickup_datetime")
)
/ 60,
2,
),
)
df = df.where(df.DurationInMin < 200)
df = df.withColumn("day_of_week", F.date_format("tpep_pickup_datetime", "E"))
df_map = df.groupBy(col(zip_plot).alias("zip_code")).agg(
F.avg("DurationInMin").alias("avg_trip_duration"),
F.count("DurationInMin").alias("cout_trips"),
F.avg("trip_distance").alias("avg_trip_distance"),
)
df_map_show = df_map.toPandas()
df_map_show.head()
nycmap = json.load(open("nyc-zip-code-tabulation-areas-polygons.geojson"))
return px.choropleth_mapbox(
df_map_show,
geojson=nycmap,
locations="zip_code",
featureidkey="properties.postalCode",
color=column_map_show,
color_continuous_scale="viridis",
mapbox_style="carto-positron",
zoom=9,
center={"lat": 40.7, "lon": -73.9},
opacity=0.7,
hover_name="zip_code",
)
if __name__ == "__main__":
app.run_server(debug=True)