Embed Python in WordPress in 2 minutes – 2 easy ways

Embed Python in WordPress in 2 minutes – 2 easy ways

Links on Code Thump may pay us an affiliate commission. Details here.

How do you embed a Python interpreter in a WordPress post?

Embed Python in HTML or WordPress in 2 minutes.

Here is a Python interpreter. You can execute Python code in it.
Try typing: print ("Hello World") and then press return.

Here’s another Python interpreter. Type 2+2 and press return:

Here’s how to do it.

Embed a Python interpreter in WordPress or HTML with Repl.it

Repl.it has changed and is not as easy as it used to be – see https://docs.replit.com/repls/embed for the latest instructions.

Here’s the quick summary:

  • Create a free account at repl.it
  • Create a new Python repl
  • Copy the address of your repl – something like https://replit.com/@codethump/pywp
  • In your WordPress post, add a custom HTML block and enter the following, using the replit.com url with your USERNAME and REPLNAME:
<iframe height="400px" width="100%" src="https://replit.com/@USERNAME/REPLNAME?lite=true&outputonly=1">

You can remove the outputonly flag if you want the full Repl.it editor embed.

The REPL (Wikipedia definition) that you get has a unique URL and is personalised to you. Whatever edits you make in the REPL are what anyone who views your page will see. Try making a change, then open your post preview in another window and you’ll see it has the same changes.

Other people’s changes will not affect your REPL. Take the URL for your REPL and open it in a new incognito window. Make some changes. Now refresh your original page (not incognito) and you won’t see those changes.

So, you can set the code that everyone else will see when they first view your page, and any changes they make will not be seen by other people.

If you play around in your REPL, remember that it will change the initial view that everyone sees, so for your post embed start with a clean fresh REPL and only enter the code you want people to see, if any. If you are showing some sample code, don’t use the “outputonly=1” option because then people won’t see your code.

By the way, just in case you’re wondering, the Python interpreter is running in Javascript in the reader’s browser. It’s not on your server and nobody can do anything with it to affect you or anyone else. The interpreter is also running in a sandbox which means people can’t even hurt themselves with it.

Embed Python in WordPress or HTML with Trinket.io

  1. Go to https://trinket.io/python
  2. Delete the sample code and enter your own (if you wish)
  3. Click Share, and choose Embed
  4. Customize the display options as you wish
  5. Copy the embed code
  6. Start a WordPress post
  7. In the block editor, add an HTML block
  8. Paste the embed code
  9. Save draft and preview your page

Again, what someone else does in their local view of the interpreter will not affect what other people see.

So there you go. Embedding an interactive Python interpreter in any HTML page or a WordPress post is as easy as that. You can embed an empty interpreter, or you can show sample code if you prefer. Anyone reading it can play around, try things out, change the code, and run the code, and that won’t affect what anyone else sees.

The code runs in Javascript in the browser, not on your server.

It’s free, it’s quick, it’s easy. The only downside is that there is some external Javascript, CSS, and images that will be loaded when the reader views your page. There’s not a lot of it, and it loads on-demand, and much of it is being served through CDNs, so it won’t be a problem for page loading speed and ought to be quite reliable for availability.

Scroll to Top