Some Bits: Nelson's log

A simple Datasette experiment

Thought I’d give Datasette a quick try. I was curious about which countries were the most and least diverse. Seemed like a good simple use case for the data exploration tool. This is my first time really using it. I am trying to use higher level tools rather than write low level code that works exactly like I want. Here’s what I did. An hour or two of tinkering and learning here, maybe a little more.

Preparing data

Gathered data from Wikipedia: diversity and population. I used this tool to extract CSVs from the tables.

Manually edited the CSVs to change the column headings to simpler things without whitespace.

Installed datasette, sqlite-utils, and csvs-to-sqlite via pip.

Ran csvs-to-sqlite *.csv diversity.db to make a database.

Merged the two tables into one by using sqlite to run create table combined as select * from diversity left join population on diversity.Country = population.Country;

Removed an extra copy of the country name with sqlite-utils transform *db combined --drop 'Country:1';

Converted the population column from string to text with this code:

$ sqlite-utils convert *db combined "Population2023" '
try: return int(value.replace(",", ""))
except: return None'

$ sqlite-utils transform *db combined --type Population2023 integer

Querying data

And with that now I have a nice little web GUI. I used it to make a report of the diversity of large European countries. Which immediately yields a lot of questions about exactly how we’re defining diversity, but that’s not Datasette’s fault.

Thoughts

Datasette is cool, a nice generic HTML front end to tabular data.

Preparing data was more fiddly than I liked. I felt like Datasette and related tools wasn’t doing a lot of work for me I couldn’t just do in sqlite. For instance, I was hoping for a GUI in datasette for doing the join between the two sources tables. It will let you do the Datasette slice-and-dice on top of any sql, so you can have an ephemeral table where you write the join yourself, but I wanted a persistent table for convenience.

The management of the population column types seemed awfully awkward. My input CSV had quoted text-looking columns for the numbers like "233,319". The code uses pandas to import CSV files, which I thought was smart enough to parse those as numbers. Maybe there’s a way to get csvs-to-sqlite to import them that way. It’d be nice if sqlite-utils convert inspected the Python type and changed the column automatically. (There’s a -transform option but I think it only works if you create a new column.)

I was hoping for some way to do a chloropleth of the diversity measures, but no one’s written that plugin. Would be easy enough to export a CSV and use some other service to visualize. There are some map visualization plugins for Datasette but they seem oriented to working with lat/lon pairs, not country names.

It’s really cool how easy it’d be to publish this little set of data to some cloud service.

I’m surprised the integer columns don’t have separators in their display! “144713314” is hard to read. This would be an easy plugin I imagine.