Skip to content

Instantly share code, notes, and snippets.

@ptmcg
Last active May 19, 2022 03:28
Show Gist options
  • Save ptmcg/b30b26fc806215793d1b99e81601aecb to your computer and use it in GitHub Desktop.
Save ptmcg/b30b26fc806215793d1b99e81601aecb to your computer and use it in GitHub Desktop.
Demonstration of CSV import using littletable in pyscript
<html>
<head>
<script defer src="https://pyscript.net/alpha/pyscript.min.js"></script>
<py-env>
- littletable
</py-env>
</head>
<body>
<h2>Iris data</h2>
<py-script>
from pyodide.http import open_url
import littletable as lt
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv"
# define column names and numeric conversion transforms
names = ["sepal-length", "sepal-width", "petal-length", "petal-width", "class"]
iris_transforms = dict.fromkeys(
["petal-length", "petal-width", "sepal-length", "sepal-width"], lt.Table.convert_numeric
)
# import the data from remote URL
iris_table = lt.Table(url).csv_import(
open_url(url), fieldnames=names, transforms=iris_transforms
)
iris_table.create_index("class")
print(iris_table.table_name)
# print summary counts by class name
print(iris_table.pivot("class")
.as_table()
.as_html(table_properties={"border": 1, "cellpadding": 2}))
</py-script>
<p>
<h2>Iris summary stats</h2>
<py-script>
# compute and print summary statistics for numeric columns
stats = iris_table.stats(
["petal-length", "petal-width", "sepal-length", "sepal-width"]
)
print(stats("stats").as_html(table_properties={"border": 1, "cellpadding": 2}))
</py-script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment