Py.Cafe

panel-org/

hvplot-explorer

Upload a csv file and explore it via the hvplot Explorer

DocsPricing
  • app.py
  • requirements.txt
app.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
import io

import hvplot.pandas
import pandas as pd

import panel as pn

pn.extension(template="fast")

pn.state.template.title = "hvPlot Explorer"

upload = pn.widgets.FileInput(name="Upload file", height=50)
select = pn.widgets.Select(
    options={
        "Penguins": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv",
        "Diamonds": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv",
        "Titanic": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/titanic.csv",
        "MPG": "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/mpg.csv",
    }
)


def add_data(event):
    b = io.BytesIO()
    upload.save(b)
    b.seek(0)
    name = ".".join(upload.filename.split(".")[:-1])
    select.options[name] = b
    select.param.trigger("options")
    select.value = b


upload.param.watch(add_data, "filename")


def explore(csv):
    df = pd.read_csv(csv)
    explorer = hvplot.explorer(df)

    def plot_code(**kwargs):
        code = f"```python\n{explorer.plot_code()}\n```"
        return pn.pane.Markdown(code, sizing_mode="stretch_width")

    return pn.Column(
        explorer, "**Code**:", pn.bind(plot_code, **explorer.param.objects())
    )


widgets = pn.Column(
    "Select an existing dataset or upload one of your own CSV files and start exploring your data.",
    pn.Row(
        select,
        upload,
    ),
).servable()

pn.panel(pn.bind(explore, select)).servable()