Skip to content

Instantly share code, notes, and snippets.

@villares
Last active September 26, 2022 00:25
Show Gist options
  • Save villares/883381721b2fc61f34000c8ce9d58a92 to your computer and use it in GitHub Desktop.
Save villares/883381721b2fc61f34000c8ce9d58a92 to your computer and use it in GitHub Desktop.
Read CSV from page and draw using py5
# 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)
# 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)
@villares
Copy link
Author

For some sites you might need this:

    user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
    headers = {'User-Agent': user_agent}
    response = urlopen(Request(url, headers=headers)) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment