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.
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.
Embed Python in WordPress or HTML with Trinket.io
- Go to https://trinket.io/python
- Delete the sample code and enter your own (if you wish)
- Click Share, and choose Embed
- Customize the display options as you wish
- Copy the embed code
- Start a WordPress post
- In the block editor, add an HTML block
- Paste the embed code
- 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.