Skip to content

elizabethsiegle/cf-python-worker-query-d1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Query a D1 Database with Cloudflare Workers in Python

  1. Create a new Cloudflare Worker from the command line with npm create cloudflare@latest. When prompted, give your project a name, select Hello World example, Hello World Worker template, and Python (beta) when asked about language.

OR

  1. git clone https://github.com/cloudflare/python-workers-examples cd python-workers-examples/01-hello npx wrangler@latest dev

  2. Replace the boilerplate code in a src/main.py with this src/main.py code.

  3. Download this quotes.csv file from https://www.kaggle.com/datasets/manann/quotes-500k?select=quotes.csv

  4. Save it to the data folder here

  5. Run python3 data/csvtosql.py

  6. Create a new D1 database in your Cloudflare dashboard D1 dashboard

  7. Import the new SQL file to D1 with npx wrangler d1 execute {YOUR-DATABASE-NAME} --remote --file=data.sql

  8. Deploy your Worker with npx wrangler@latest deploy

  9. Get a random quote from the database by visiting your deployed worker in the browser!deployed app