Django and htmx

The 'Django' and the 'htmx' logo beside each other.

I gave a talk last night at the Auckland chapter meetup of Python New Zealand. The subject was the use of htmx with Django .

The JavaScript library htmx allows ‘native app like’ user experience for a Django project. Instead of refreshing the entire page, only the parts of the screen that actually need updating are refreshed. This eliminates those pesky flashes and reloads you get with conventional page refreshes. The result? A much smoother interface..

Now, normally, achieving this level of interactivity would require a full-fledged JavaScript-based frontend, along with the corresponding deep dive into React, Vue, or whichever framework is trending this month. But with htmx, you can get similar results by making relatively minor changes to your existing Django templates and views. No new framework, no need to sell your soul to JavaScript (at least not entirely) and you do away with large amounts of code serializing and de-serializing data.

In a Django context this is all made easier with the help of the django-htmx add-on (written by Adam Johnson). It provides the tools you need to integrate htmx into your Django project. In my presentation, I shared an overview of htmx and how combining it with django-htmx can deliver a smooth, engaging user experience—without the need to build a full-on JavaScript frontend or expose a mountain of API endpoints to service it.

That said, I’m not suggesting htmx is a silver bullet. There are plenty of scenarios where a “proper” JavaScript framework—be it React/Next.js, Vue, or Svelte—makes more sense. But I’m excited to explore more about where htmx fits into the Django ecosystem and how it can simplify things in the right contexts

Here are my slides from last night.

Sssh ! – Cloudflare Secrets are Environment Variables

Introduction

When working with Cloudflare Workers you sometimes need to use a feature which Cloudflare calls “Secrets” .

Creating a Secret

To create a “Secret” called “FOO” you do this …

$ wrangler secret put FOO

… and you’ll get prompted for what value you want to associate with ‘FOO’.

Viewing a Secret via the Dashboard

The strange thing is that if you go into your Cloudflare dashboard there’s no sign of any “Secret”.

After a good deal of digging around I found the “Secret” I had created but it’s not referred to as “Secret” it’s labelled “Environment Variables”. That’s actually a better name for it than “Secret” but it’s pretty confusing and there’s no reference that I can find for it in the Cloudflare documentation.

In Detail

So just to be clear this is how you find your “Secret” …

  • Logon to your Cloudflare dashboard
  • Select ‘Workers’
  • In the resulting list of ‘Workers’ select the one you create a ‘Secret’ for.
  • In the resulting screen select “Settings”
  • There are two options within “Settings”, they are “General” and “Variables”, select “Variables”
  • Your “Secret” is now visible under the heading “Environmental Variables”.

Cloudflare Workers – using KV in ‘dev’ or ‘preview’

Overview

When developing Cloudflare Workers which make use of Cloudflare KV there’s an error message you might get which reads like this :

Error: In order to preview a worker with KV namespaces, you must designate a preview_id in your configuration file for each KV namespace you'd like to preview.

What’s going on ?

There’s a couple of things it’s useful to know when resolving this, and as far as I can see it’s not clearly documented in the Cloudflare Workers, or KV documentation.

If you’re seeing this you need to go and inspect your wrangler.toml file. You probably have something that looks like this :

kv_namespaces = [
{ binding = "FOO", id = "bb12cc34dd56eebb12cc34dd56eebb12" }
]

Or you may have no kv_name_spaces entry at all. Either way this is how to resolve the situation.

How to fix this

Execute the following command.

$ wrangler kv:namespace create "FOO" --preview

If you do have an existing entry for kv_namespaces be sure to use the binding property value that’s already in use, in this example ‘FOO’, otherwise use whatever name you want as your namespace name.

Once you’ve executed that command you should see output which looks like the following :

Add the following to your configuration file in your kv_namespaces array:{ binding = "FOO", preview_id = "d94ab232f1ef23acdeaeef0add746a48", id = "bb12cc34dd56eebb12cc34dd56eebb12" }

Go ahead and place that in the wrangler.toml and you should find the …

Error: In order to preview a worker with KV namespaces, you must designate a preview_id in your configuration file for each KV namespace you'd like to preview.

… message doesn’t appear any longer when using wrangler dev or wrangler preview.

Fake properties

The Fake library has a whole lot of built in properties that you can use to generate fake data for testing, etc. Unfortunately the documentation hasn’t kept up with the available properties so here’s a list of most of them as they are in the current release. It’s ‘most of them’ because some properties, such as ‘tar’, don’t lend themselves to easily being on a web page.

Once I get some time I’ll do a pull request for the documentation for the actual project so that this page will become redundant.

abcdef

fake.aba065144370
fake.address34370 Pace Radial Nortontown, NC 29290
fake.administrative_unitSouth Carolina
fake.am_pmPM
fake.android_platform_tok enAndroid 3.2.6
fake.ascii_company_emaildsmith@watts.org
fake.ascii_emailchristineporter@gmail.com
fake.ascii_free_emailjensenalyssa@gmail.com
fake.ascii_safe_emailchristopher46@example.org
fake.bank_countryGB
fake.bbanQIEV11003029338014
fake.booleanTrue
fake.bothify87 gf
fake.bsenable collaborative applications
fake.building_number9389
fake.catch_phraseProfit-focused analyzing collaboration
fake.centuryXV
fake.chromeMozilla/5.0 (Windows NT 5.1) AppleWebKit/531.0 (KHTML, like Gecko) Chrome/26.0.813.0 Safari/531.0
fake.cityPetersonchester
fake.city_spanfixEast
fake.city_suffixmouth
fake.color#58910e
fake.color_nameTomato
fake.companyThompson LLC
fake.company_emailyjohnson@mcneil-johnson.com
fake.company_suffixand Sons
fake.coordinate39.909401
fake.countryArmenia
fake.country_calling_code+1 869
fake.country_codeCG
fake.credit_card_expire04/30
fake.credit_card_fullVISA 19 digit John Henderson 4388782482398195841 03/29 CVC: 602
fake.credit_card_number2238435833272261
fake.credit_card_providerJCB 15 digit
fake.credit_card_security_code796
fake.cryptocurrency(‘ETH’, ‘Ethereum’)
fake.cryptocurrency_codeXPM
fake.cryptocurrency_nameBitcoin
fake.csv“Robert Anderson”,”06446 Nathan Forks Lanefurt, NY 27437″ “Robert Anderson”,”92218 Cassandra Locks Lake Todd, SC 84356″ “Bradley Johnston”,”9518 Jason Centers Suite 333 West Jefferyberg, UT 20673″ “Austin Hudson”,”27678 Lauren Estate Scottview, WI 21027″ “Ryan Hart”,”834 Don Crest Apt. 132 South Chelsea, MO 06160″ “Joel Bullock”,”65224 Samuel Island Apt. 999 Yatesmouth, MS 41571″ “Joy Leonard”,”950 Gentry Place Port Bryan, WI 91489″ “Dr. Mary Lawrence”,”8994 Anna Mountain Owenburgh, NY 90689″ “Sarah Campbell”,”7866 Christopher Trace Suite 629 North Tiffanyport, CA 91255″ “Eric Allen”,”94927 Lisa Knoll Apt. 346 West Christopherberg, NV 93152″
fake.currency(‘USD’, ‘United States dollar’)
fake.currency_codeSCR
fake.currency_nameJordanian dinar
fake.currency_symbol$
fake.current_countryUnited States
fake.current_country_codeUS
fake.date2011-05-11
fake.date_between2013-09-23
fake.date_between_dates2021-08-31
fake.date_object2008-10-27
fake.date_of_birth1936-09-02
fake.date_this_century2004-10-08
fake.date_this_decade2021-01-06
fake.date_this_month2021-08-25
fake.date_this_year2021-05-17
fake.date_time2020-02-28 00:21:01
fake.date_time_ad1785-05-08 19:44:16
fake.date_time_between1999-08-12 07:04:19
fake.date_time_between_dates2021-08-31 14:18:23
fake.date_time_this_century2010-07-05 08:40:01
fake.date_time_this_decade2021-01-04 19:55:52
fake.date_time_this_month2021-08-27 11:34:48
fake.date_time_this_year2021-04-10 09:48:15
fake.day_of_month07
fake.day_of_weekWednesday
fake.dgaerlwjrtlnkidlymxgysbpdyldlaaeqr.com
fake.domain_nameallen.com
fake.domain_wordmoore
fake.dsv“Jeremy Gordon”,”267 Crystal River Apt. 657 North Shaneborough, FL 36124″ “Charles Campbell”,”35681 Keith Road Lake Dustinchester, WI 72084″ “Jennifer Robinson”,”153 Jean Court New Jessica, ID 88924″ “Mrs. Alison Black DVM”,”USS Parker FPO AA 81626″ “Robert Thomas”,”501 Charles Parkways Martinland, TX 36101″ “Megan Lopez”,”7203 Williams Mill Suite 707 Kaylahaven, NV 86377″ “Karen Shaw”,”873 David Mountains Suite 355 Scottville, MI 03858″ “Lisa Mitchell”,”033 Andrew Mountains Angelicaberg, NJ 46464″ “Jennifer Bailey”,”9698 Lynn Overpass Suite 225 Williamsland, NH 86156″ “Tiffany Powers”,”308 Huerta Crescent Kirbymouth, CO 74505″
fake.ean6888039620628
fake.ean133507382888358
fake.ean816591516
fake.ein39-0323987
fake.emailhdavis@gates-oneill.com
fake.file_extensionmov
fake.file_namethe.png
fake.file_path/cost/impact.tiff
fake.firefoxMozilla/5.0 (Windows NT 4.0; sa-IN; rv:1.9.1.20) Gecko/2013-09-02 18:58:42 Firefox/15.0
fake.first_nameBrittany
fake.first_name_femaleCarrie
fake.first_name_maleVincent
fake.first_name_nonbinaryGregory
fake.fixed_widthWalter Gomez 1 Heather Larson 0 Michael Cabrera 17 Marc Benitez 0 Kelly Novak 8 Tyler Scott 9 Daniel Walker Jr. 14 Lisa Gonzalez 5 Kerry Clark 14 Alan King 12
fake.free_emailtallison@yahoo.com
fake.free_email_domainhotmail.com
fake.future_date2021-09-12
fake.future_datetime2021-09-10 12:53:08

ghijkl

fake.hex_color#de5fee
fake.hexifyc557
fake.hostnamedb-31.walker.info
fake.http_methodPATCH
fake.iana_id4100134
fake.ibanGB19TCHN98296480355376
fake.image_urlhttps://placeimg.com/554/722/any
fake.internet_explorerMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.2; Trident/4.1)
fake.invalid_ssn018-00-8384
fake.ios_platform_tokeniPhone; CPU iPhone OS 10_3_3 like Mac OS X
fake.ipv454.145.129.32
fake.ipv4_network_classa
fake.ipv4_private192.168.49.103
fake.ipv4_public147.151.70.102
fake.ipv6926b:67b3:8db1:ed20:c309:72db:cc03:8dba
fake.isbn100-686-28575-1
fake.isbn13978-0-89646-269-4
fake.iso86011991-10-14T02:54:22
fake.itemsodict_items([(‘en_US’, )])
fake.itin907-77-4407
fake.jobSurveyor, minerals
fake.json[{“name”: “Mario Gonzalez PhD”, “residency”: “29582 Alan Plain Suite 496\nEast Peggy, FL 10548”}, {“name”: “Ashley Koch”, “residency”: “637 Smith Spur Suite 465\nLake Anthony, WI 24939”}, {“name”: “Matthew Lopez”, “residency”: “5407 Jacqueline Underpass Apt. 076\nWest Katie, MO 83289”}, {“name”: “Kaylee Stone PhD”, “residency”: “970 Cortez Rest\nSheltonville, NC 55621”}, {“name”: “James Smith”, “residency”: “131 Mullen Vista Suite 147\nJordanshire, KY 62332”}, {“name”: “Hannah Nichols”, “residency”: “USCGC Nguyen\nFPO AA 29320”}, {“name”: “Crystal Snyder”, “residency”: “97306 Shawn Causeway\nWallacetown, IA 12768”}, {“name”: “Kimberly Potter”, “residency”: “0590 Taylor Trail\nZacharyburgh, MS 33137”}, {“name”: “Bonnie Byrd”, “residency”: “33542 April Union\nJuliahaven, AR 98354”}, {“name”: “Judith Jenkins”, “residency”: “15746 Davis Trafficway Suite 166\nLake John, NJ 51448”}]
fake.language_codeor
fake.language_nameTatar
fake.last_nameWright
fake.last_name_femaleHoffman
fake.last_name_maleHernandez
fake.last_name_nonbinaryGomez
fake.latitude21.7625585
fake.latlng(Decimal(‘63.615652’), Decimal(‘-98.149124’))
fake.lexifyEnyd
fake.license_platePVZ-9007
fake.linux_platform_tokenX11; Linux i686
fake.linux_processori686
fake.local_latlng(‘39.72943’, ‘-104.83192’, ‘Aurora’, ‘US’, ‘America/Denver’)
fake.localeis_IS
fake.localized_ean0016388652144
fake.localized_ean130029053440380
fake.localized_ean811941910
fake.location_on_land(‘35.95664’, ‘36.7138’, ‘Binnish’, ‘SY’, ‘Asia/Damascus’)
fake.longitude-169.728120

mnopq

fake.mac_address0f:0d:c0:15:79:32
fake.mac_platform_tokenMacintosh; U; PPC Mac OS X 10 12_2
fake.mac_processorPPC
fake.md5e37e6d022a48d7e0f802d027032b04aa
fake.military_apoPSC 5595, Box 6692
fake.military_dpoUnit 6288 Box 3051
fake.military_shipUSCGC
fake.military_stateAA
fake.mime_typeimage/gif
fake.month03
fake.month_nameMarch
fake.msisdn5331986894301
fake.nameTrevor Rich
fake.name_femaleAnn Fuentes
fake.name_maleBryce Griffith
fake.name_nonbinarySara Scott
fake.nic_handleRLQ8-FAKE
fake.nic_handles[‘KS25-OJUC’]
fake.null_booleanTrue
fake.numerify652
fake.operaOpera/8.44.(Windows NT 10.0; unm-US) spansto/2.9.164 Version/10.00
fake.paragraphClearly require father manage own role. Choose friend fish season city provide rate. Treat go raise clear quickly.
fake.paragraphs[‘Beyond ready step begin region. Development our after throw professor against short.’, ‘Let eye they again. Early next personal happen generation forget fly. Group any future message someone easy.’, ‘Season Mrs star study easy write. Their chance standard yeah hotel firm why. Ask any chance week agree.’]
fake.passwordLVTj1IziD+
fake.past_date2021-08-11
fake.past_datetime2021-08-16 15:06:19
fake.phone_number734.060.8523
fake.port_number22223
fake.postalcode55402
fake.postalcode_in_state97715
fake.postalcode_plus447166-8270
fake.postcode42307
fake.postcode_in_state23207
fake.spanfixMrs.
fake.spanfix_femaleMrs.
fake.spanfix_maleMr.
fake.spanfix_nonbinaryMx.
fake.pricetag$4,813.75
fake.profile{‘job’: ‘Dentist’, ‘company’: ‘Francis Group’, ‘ssn’: ‘179-03-3184’, ‘residence’: ‘73675 Mccormick Vista\nPort Amybury, WA 24899’, ‘current_location’: (Decimal(‘-7.4860615’), Decimal(‘-106.095259’)), ‘blood_group’: ‘A+’, ‘website’: [‘http://www.johnson-smith.com/’, ‘http://www.bates.biz/’, ‘https://www.gomez-moses.org/’, ‘http://yates.biz/’], ‘username’: ‘melissabailey’, ‘name’: ‘Jennifer Williams’, ‘sex’: ‘F’, ‘address’: ‘PSC 5931, Box 4914\nAPO AE 42669’, ‘mail’: ‘cynthiaadams@yahoo.com’, ‘birthdate’: datetime.date(1981, 8, 27)}
fake.psv“Michael Wells”|”7347 Frank Village New Jose, AZ 12787” “Alex Johnson”|”508 Cole Tunnel Suite 639 Williamburgh, NE 33390” “Joseph Meadows”|”USS Blanchard FPO AA 66097” “Laurie Burke”|”9272 Carrie Flats West Anthonyville, TX 27912” “Traci Powell”|”488 Jennifer Mountains Davidfurt, KS 50099” “Sharon Cross”|”25871 Caroline Trafficway Apt. 966 South Kennethshire, IA 59844” “Eric Leblanc”|”587 Amy Tunnel Suite 573 Elizabethland, SD 06744” “Scott Campbell”|”PSC 0434, Box 1184 APO AE 08365” “John King”|”756 Shirley Neck Suite 864 New Rebecca, KY 62527” “Dale Young”|”2676 Wagner Mount Feliciaside, DE 66301”
fake.pyboolFalse
fake.pydecimal283459433.55292
fake.pydict{‘hand’: 8833, ‘current’: 7827, ‘special’: 7770, ‘right’: ‘ejJjehnAuwvdlyTjwhZA’, ‘company’: Decimal(‘-3.37239102590394’), ‘wife’: ‘https://www.meyer.biz/search/blog/homepage.php’, ‘read’: -210.856158532971, ‘people’: ‘cVfoGVuarhFNShZuKLXr’, ‘himself’: ‘XKOGonEBVzjrHVKFfJHY’, ‘anything’: -32071535121256.6}
fake.pyfloat950227457009.336
fake.pyint8293
fake.pyiterable{Decimal(‘-8806.53365815814’), Decimal(‘8705691525.92929’), 1409, ‘yjvITyupRZKQdrvgRmoL’, Decimal(‘-10619041.6630978’), ‘cDcYRiTROjcCJlHbINJx’, ‘BLWhqHBTNERMqXFtAkat’, 2326, ‘DaQNFvIZlqVJoniJwOPk’, datetime.datetime(1973, 8, 27, 4, 36, 6), 8445, 702, ‘wlang@anderson.info’}
fake.pylist[‘ejpxcIdfbzllsvZHPLId’, ‘QndTRjppgdrZOdhfnYnO’, ‘KfdeVzpvzuputwFUgqMx’, ‘QQsnxIJQkijDnsKxrJpz’, ‘hKGItjIIdGHUCBclStjC’, 7302, ‘uFeYreqpWPSsmOoPBEDb’]
fake.pyset{datetime.datetime(1974, 2, 19, 7, 4, 13), ‘nnZkVSwOWUMVqePHxCxA’, datetime.datetime(1981, 12, 19, 13, 20, 27), ‘BZMdQMjsTeghLPjaHomR’, ‘rosalessteven@yahoo.com’, ‘MNYAGpgBszBagkYpGtJt’, 4176, datetime.datetime(1985, 4, 1, 19, 51, 9), datetime.datetime(1976, 6, 18, 23, 30, 49)}
fake.pystrwcCPmxijLVvyojRYBPJE
fake.pystr_formatf1-4015059C
fake.pystruct([datetime.datetime(1980, 4, 1, 13, 0, 16), ‘david84@melendez.com’, Decimal(‘-3822.5902447118’), 505, ‘http://thomas.com/app/wp-content/main/category.html’, ‘qrmqLjkCfEvQkovBJqWB’, ‘XFSVfdpSThWPwRYEgChR’, ‘rZXTPcTlBhXFpUPtjYKg’, 6427, 256], {‘plan’: 6043, ‘arm’: datetime.datetime(1989, 10, 18, 0, 33, 35), ‘instead’: 8979, ‘investment’: ‘ypatterson@yahoo.com’, ‘way’: ‘JmxFREbhurEezTaOPPaQ’, ‘course’: 3666, ‘together’: -25037940576938.5, ‘out’: ‘RWvtbaDPDhbRlpTfNwCi’, ‘which’: ‘eQDuYYNszgRkECCnuNLR’, ‘debate’: ‘RYoqomPMrMAxXSuknxib’}, {‘agent’: {0: ‘fxurbxyfmmaWqWeNdvkY’, 1: [Decimal(‘-597725131.721244’), 2022, Decimal(‘17500917128720.6’)], 2: {0: Decimal(‘-50625135849541.7’), 1: ‘https://ingram.com/explore/register.htm’, 2: [‘hJcdcViZsKFddoWuOobw’, ‘eSYNTMqwgfQDXxzcFKoR’]}}, ‘even’: {1: 5101, 2: [‘PYSDbeVzoDmyQSyoBGzV’, ‘NnsCoaXfXixyFAuXajkS’, ‘http://brown.info/about/’], 3: {1: -72034.2200775236, 2: ‘AFGpAZHRcIwzZFIQUict’, 3: [‘DrxNENknLuoHfeXNJBBN’, 3567]}}, ‘night’: {2: datetime.datetime(1981, 6, 13, 19, 52, 7), 3: [1613, 3830, 3397], 4: {2: ‘gvYhtpPlzGKymlHEAPXi’, 3: ‘crystal87@hotmail.com’, 4: [‘sherri57@gmail.com’, ‘PgTLrcujpLRpfSRrjeIr’]}}, ‘style’: {3: -4829471750612.41, 4: [datetime.datetime(2017, 12, 5, 22, 23, 37), ‘PNuOnabhqgoTLEYOElKe’, 5705], 5: {3: 5.87284654416101, 4: ‘fuDEUjYmACKVITbkfuKp’, 5: [‘https://roman-jones.info/’, ‘xVJiaRgXUzdtTQiBDAnU’]}}, ‘third’: {4: Decimal(‘97529477.921929’), 5: [‘LpDHPEMhMuJWDdbWQpnv’, ‘ipnbaCxOSxxzxepMDBLF’, 7342], 6: {4: ‘TQgTYawMgBKLuoXHzakD’, 5: 448.838835096874, 6: [datetime.datetime(2014, 7, 16, 13, 41, 36), 4799]}}, ‘stop’: {5: 2501, 6: [datetime.datetime(1982, 4, 6, 13, 19, 37), ‘udiWbsSpMAkJyapJrhDe’, datetime.datetime(1993, 4, 30, 0, 22, 33)], 7: {5: 2691, 6: ‘http://wilson.com/category/index/’, 7: [‘iuWtgIJYQQqRGydMxxyo’, datetime.datetime(2016, 12, 22, 17, 21, 1)]}}, ‘wide’: {6: ‘AbPxUgvKlsMJTkZjLfim’, 7: [‘https://miller-le.com/categories/posts/main/’, ‘QVyTszErARaoNdqBLphN’, ‘IOzCAeUVnGfFPJjKnQri’], 8: {6: ‘umartinez@yahoo.com’, 7: ‘sandra35@anderson.com’, 8: [‘http://petty-adams.com/posts/home.html’, 93]}}, ‘now’: {7: 2207, 8: [1826, ‘QzYWRrpbSpGfPlkEXovR’, ‘KBAJDPCyioQOnazONyTf’], 9: {7: Decimal(‘-30579.780610314’), 8: Decimal(‘-4.6545327801111’), 9: [‘XJduICTRIpshqOrZeLBA’, ‘barrettlisa@stewart.com’]}}, ‘suffer’: {8: datetime.datetime(2020, 8, 24, 12, 35, 36), 9: [‘http://www.smith.info/terms/’, 9654, ‘EIWqCoPFCCstjGPCMpuR’], 10: {8: ‘melissa83@yahoo.com’, 9: ‘http://www.collins-peterson.net/post.html’, 10: [‘morrismichael@gmail.com’, -5.79789665945564]}}, ‘particularly’: {9: ‘MJnLmgcHHTsZxAbYuojp’, 10: [datetime.datetime(1972, 1, 9, 3, 19, 45), ‘LhuhEofDLxujPHhdjCpw’, Decimal(‘1.87685014697517’)], 11: {9: Decimal(‘683384467.700707’), 10: ‘WdAMRIqSNHCYBbBjnxKv’, 11: [-8457.45804024019, ‘dhsSWHdDtCgfeBHfjOML’]}}})
fake.pytimezonetzfile(‘/usr/share/zoneinfo/Asia/Samarkand’)
fake.pytuple(3709, ‘aguilarkatherine@gmail.com’, ‘YbpImntiaTkUrRdSJhWX’, ‘CyUtlRZYMRreaIVezaeK’, datetime.datetime(2012, 12, 28, 13, 29, 28), Decimal(‘18938402.3875171’), ‘kvIYZZIETCbHrOinOmmo’, 8732, ‘lgUAAtTDfCJbMJmiqUSU’, ‘cXoPnrcnMvSbWFyrdIVL’)
fake.random_choices[‘c’, ‘c’]
fake.random_digit0
fake.random_digit_not_null4
fake.random_digit_not_null_or_empty7
fake.random_digit_or_empty
fake.random_elementa
fake.random_elements[‘c’, ‘b’]
fake.random_int6496
fake.random_letterr
fake.random_letters[‘x’, ‘J’, ‘b’, ‘i’, ‘T’, ‘L’, ‘Z’, ‘T’, ‘Q’, ‘X’, ‘a’, ‘n’, ‘L’, ‘T’, ‘Q’, ‘Y’]
fake.random_lowercase_lettere
fake.random_number78977
fake.random_sample[‘c’, ‘b’, ‘a’]
fake.random_uppercase_letterD
fake.randomize_nb_elements11
fake.rgb_color220,13,24
fake.rgb_css_colorrgb(236,165,162)
fake.ripe_idORG-OBH71582-RIPE

stuvw

fake.safariMozilla/5.0 (Macintosh; Intel Mac OS X 10 10_4 rv:2.0; sq-AL) AppleWebKit/531.9.3 (KHTML, like Gecko) Version/4.0 Safari/531.9.3
fake.safe_color_namefuchsia
fake.safe_domain_nameexample.net
fake.safe_emailmatthewholland@example.net
fake.safe_hex_color#449900
fake.secondary_addressSuite 650
fake.seed_instanceNone
fake.sentenceRise kid cell involve suggest support.
fake.sentences[‘Stock our bar.’, ‘spanssure capital know such Congress particularly fly.’, ‘Eye charge few their five.’]
fake.sha1952a4535af8c83e2fa92ab889686a3ca1f1f9115
fake.sha2561694a7d23051b104393e10d31ff30a5f1e46b23b19b589e1dd834b577bb81e33
fake.simple_profile{‘username’: ‘burtonnancy’, ‘name’: ‘Joseph Thompson’, ‘sex’: ‘M’, ‘address’: ‘94354 Perez Cliffs Apt. 299\nMartintown, NY 76630’, ‘mail’: ‘wmeyer@yahoo.com’, ‘birthdate’: datetime.date(1908, 6, 18)}
fake.sluginstead-ever
fake.ssn629-76-6588
fake.stateCalifornia
fake.state_abbrCA
fake.street_address1784 Wells Passage Suite 054
fake.street_nameKeith Island
fake.street_suffixFords
fake.suffixDDS
fake.suffix_femaleMD
fake.suffix_maleDDS
fake.suffix_nonbinaryII
fake.swiftTCXZGBR3SLR
fake.swift11ZQAVGBM0VGJ
fake.swift8KAFGGB02
fake.time03:05:43
fake.time_delta0:00:00
fake.time_object09:20:55
fake.time_series
fake.timezoneAmerica/Nassau
fake.tldorg
fake.tsv“Charles Chung” “7239 George Tunnel Suite 851 Watkinsfurt, KY 21713” “Jessica Curtis” “130 James Mill Suite 888 Michaelchester, PA 87290” “Diane Smith MD” “4551 John Drive Suite 511 Jasonmouth, MD 40664” “Courtney Cunningham” “73075 Jarvis Inlet Roseside, OR 61500” “Karen Hernandez DVM” “05867 Davis Corners Suite 154 Williammouth, OK 75823” “Brian Morris” “89547 Abigail Locks Kevinstad, MT 04155” “Jennifer Hart” “9977 Tran Extensions Apt. 058 West Brenda, NH 57053” “Jacqueline Moore” “44492 Mary Village Apt. 995 Lake Lonnie, OH 69299” “Gregory Brady” “83333 Davis Common Port Calvin, RI 70021” “Tiffany Wiggins” “9373 Holloway Causeway Oconnorton, CT 62855”
fake.unix_device/dev/vde
fake.unix_partition/dev/vdw0
fake.unix_time20580974
fake.upc_a970061018989
fake.upc_e14802928
fake.urihttps://barnes.com/faq/
fake.uri_extension.jsp
fake.uri_pageprivacy
fake.uri_pathapp
fake.urlhttp://www.soto-davenport.com/
fake.user_agentMozilla/5.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/4.1)
fake.user_nameerin21
fake.uuid438fbd9a9-0b05-49c3-937e-5856a803548b
fake.windows_platform_tokenWindows 95
fake.wordstand
fake.words[‘chance’, ‘especially’, ‘center’]

xyz

fake.year1994
fake.zipcode47750
fake.zipcode_in_state86077
fake.zipcode_plus444500-9458

‘Behave’ functional testing and the context object

Saving data to the context object

When using the behave framework for functional testing it’s often necessary to set some data at the start of the testing and be able to make use of it throughout the test run.

The context object (api of the object) is there to help you however I don’t think the current documentation makes it as it clear as it should be how you do this.

An Example

Within the environment.py (placed in your features directory) you can define an event handler to run before any testing is done, that event handler is called before_all below is an example of using before_all to set an arbitrary value which is then available through out the test run.

#This event runs before any of your tests
def before_all(context):
   #Choose whatever property name you like and assign 
   #a value to it. In this case I have chosen 'colour'
   #and assigned "RED" to it.
   context.colour = "RED"

   #You can assign any python object to one of these
   #properties, here's an example of assigning a dictionary
   #to one
   context.weather = {'temp-centigrade': '21', 'wind-speed-kmh': 5}

Warning

The context object is used by behave for other purposes so whatever property you set you need to check that it’s not one that is used by behave internally.

There’s a list of the internally used properties here so you can avoid them.

My opinon

To ensure you don’t accidentally stomp on one of the predefined properties it might be best to use only one top level property (eg context.my_project_name) and assign an empty dictionary to that. You can then store everything you want within the dictionary at context.my_project_name).

Using pipenv to install from github

Summary

I’ve taken to using pipenv (“Python Development Workflow for Humans”) recently and so I frequently come across things I haven’t done previously and need to figure out how to. Todays’s is using pipenv to install a package from a github repository.

How do you do that ?

So, short and sweet, here’s how. In my case I have forked the repos ‘behave-web-api‘ and I want to install my forked version into my current project.

$ pipenv install -e git+ssh://git@github.com/shearichard/behave-web-api.git#egg=behave-web-api

Notice the git+ssh is used as the scheme of the url. Also notice that the egg=behave-web-api (with the value after the equals sign changed to whatever package you’re dealing with) is necessary .

References

References

The pipenv doco for this is comprehensive but because it’s so comprehensive is a little more than you might need in a lot of cases. I also found the note written to himself by Koen Woortman a useful pointer.

The evolution of an arrow function

Summary

As a reminder to me, and I hope, a help to others here are the various ways in which JavaScript arrow functions can be used to reduce the amount of code you write.

Less is more ?

Here I start with an old fashioned JavaScript function being used as an argument to the map function and then step by step reduce the amount of the code to take full advantage of the possibilities of JavaScript arrow functions. Not all of these steps would be possible in all circumstances.

//Test data is an array of integers
const nums = [1,2,3,4,5,6,7,8,9,10]

//We start by using a 'traditional' 
//function definition as our argument 
//to 'map'
const first_approach = nums.map(
  function(n){
    return num * 2
  }
)

//Convert the 'traditional' function 
//definition to an arrow function
const second_approach = nums.map((n) => {
    return n * 2
})

//When there's only one argument to the 
//function we can remove the brackets 
//around the argument, so 'n' is no 
//longer bracketed
const third_approach = nums.map(n => {
    return n * 2
})

//Because only one thing is being 
//returned the processing can all be 
//placed on one line ...
const fourth_approach = nums.map(n => { return n * 2 })

//Take advantage of the implicit return 
//within an arrow function so we no longer 
//need to use 'return' and, in the process, 
//do away with the curly brackets.
const fifth_approach = nums.map(n => n * 2 )


Summary

So just to summarise, in the right circumstances it’s possible to go from this …

//Longest version
const first_approach = nums.map(
  function(n){
    return num * 2
  }
)

… to this .

//Shortest version
const fifth_approach = nums.map(n => n * 2 )

Full Details

Fuller details about when all of these optimisations are possible are available on the excellent MDN Arrow Function page.

VirtualBox – blank screen

I just had a VirtualBox client machine start but the window it was running in was blank. I fiddled around for a while and then waited. Still nothing in the client machine window but blackness.

I looked at the logs held by VirtualBox and there was nothing that looked unusual, although there’s a lot in those logs and I never normally read them so plenty of scope for overlooking something significant.

Then I tried to ssh into the client machine, fully expecting this to fail. It worked!

Then I found this web page and it mentioned Display Memory. Well I knew the display memory had been good the last time I used the VM so I couldn’t see how that was a problem ? But in fact when I went into settings I discovered the Display Memory had been changed to 1MB. My sticky fingers at work I guess ? Regardless I gave it 128MB and closed down the machine through the SSH session, then restarted and, bang !, the machine’s display was back working as it should have been.

Long story short if your Virtualbox machine is blank go and check the Display Memory settings.

Don’t forget when starting with django-sockpuppet

Today I’ve started my first django-sockpuppet project ( https://pypi.org/project/django-sockpuppet/ ).

If you’re using pipenv the first thing you need to do is

pipenv install django-sockpuppet

but I found that command failed when it came to installing the dependency on Twisted https://pypi.org/project/Twisted/ .

The problem was that I had started using a new virtual machine for development and it was lacking libraries which Twisted depends on. To be clear this is an Ubuntu 20.x machine and this problem may be specific to that environment.

So if you’re using django-sockpuppet and you have problems installing it try executing the following.

sudo apt-get install python3-dev
sudo apt-get install libevent-dev

This is pretty standard stuff for a new machine but easy to forget if you don’t change very often.