Last active
September 26, 2022 00:25
-
-
Save villares/883381721b2fc61f34000c8ce9d58a92 to your computer and use it in GitHub Desktop.
Read CSV from page and draw using py5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using Paul McGuire's littletable https://pypi.org/project/littletable/ | |
# Needs py5 imported mode https://py5.ixora.io for drawing | |
import csv | |
import littletable as lt | |
# Reading a CSV from a GitHub repo, but it can be used | |
# to read from a Google Spreadsheet if you use the 'publish' feature | |
url = "https://raw.githubusercontent.com/villares/Resources-for-teaching-programming/main/II%20-%20Books%20%26%20References.csv" # change to your own! | |
def setup(): | |
size(1000, 600) | |
fill(0) | |
text_size(8) | |
data = lt.Table().csv_import(url) | |
cols = data.info()['fields'] | |
rows = data | |
line(0, 20, width, 20) | |
for i, col in enumerate(cols): | |
w = 20 + 20 * i * i # too clever by half, you should think of something else | |
text(col, w, 15) | |
for j, row in enumerate(rows): | |
text(getattr(row, col), w, 40 + j * 20) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needs py5 imported mode https://py5.ixora.io for drawing | |
import csv | |
from urllib.request import Request, urlopen | |
# Reading a CSV from a GitHub repo, but it can be used | |
# to read from a Google Spreadsheet if you use the 'publish' feature | |
url = "https://raw.githubusercontent.com/villares/Resources-for-teaching-programming/main/II%20-%20Books%20%26%20References.csv" # change to your own! | |
def setup(): | |
size(1000, 600) | |
fill(0) | |
text_size(8) | |
response = urlopen(Request(url)) | |
content = response.read().decode('utf-8') | |
data = csv.DictReader(content.splitlines()) | |
cols = data.fieldnames | |
rows = list(data) | |
line(0, 20, width, 20) | |
for i, col in enumerate(cols): | |
w = 20 + 20 * i * i # too clever by half, you should think of something else | |
text(col, w, 15) | |
for j, row in enumerate(rows): | |
text(row[col], w, 40 + j * 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some sites you might need this: