-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpart2_ex1_solution_app.py
More file actions
55 lines (47 loc) · 1.39 KB
/
part2_ex1_solution_app.py
File metadata and controls
55 lines (47 loc) · 1.39 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
# PART 2 - Exercise 1 - Solution
# //////////////////////////////
import requests
import pandas as pd
from io import StringIO
from shiny import App, ui, render
# Get the data and process it
url = "https://data.opendatasoft.com/api/explore/v2.1/catalog/datasets/cats-in-movies@public/exports/csv"
resp = requests.get(url)
data = pd.read_csv(StringIO(resp.content.decode("UTF-8")), sep=";").sort_values(
by=["title"]
)
# UI
app_ui = ui.page_fluid(
ui.panel_title("Movies with cats"),
ui.row(
ui.column(
4,
ui.input_select("movie", "Movie", choices=data["title"].tolist()),
ui.output_ui("img"),
),
ui.column(
8,
ui.input_slider(
"era",
"Era",
min=min(data["year"]),
max=max(data["year"]),
value=[min(data["year"]), max(data["year"])],
),
ui.output_data_frame("tbl"),
),
),
)
# SERVER
def server(input, output, session):
@render.ui
def img():
return ui.img(src=data[data["title"] == input.movie()]["url_poster"].values[0])
@render.data_frame
def tbl():
table = data[
(data["year"] >= input.era()[0]) & (data["year"] <= input.era()[1])
]
table = table[["year", "title", "produced_by", "directed_by"]]
return table
app = App(app_ui, server)