Want to make your website more interactive? Embed a Python interpreter into your website!
This does not only increase the interactivity and usability of your website — and create happy users for your web business. It also increases the average time spent on your website because people will try out things.
So it’s actually a good idea in terms of “search engine optimization” because Google and other search engines track the time spent on your site and use it as a ranking criterion
How does an embedded Python interpreter look like?
Here’s an example of an embedded Python interpreter:
Try it yourself. You can modify the code in the embedded interpreter. Then, you can run and play around with the code. (Can you find the bug?)
How to embed a Python interpreter like this in HTML?
The previous embedded Python interpreter was created with the great web service Trinket.io.
Simply follow the step-by-step guide to embed your own Trinket interpreter in WordPress or any other web framework that allows you to modify the HTML code.
- Visit Trinket.io and wait until the website is fully loaded (it may take a few seconds before the interpreter appears).
- Scroll down until you see the interpreter and modify the code as you please.
- Now click on the arrow down and click the “Embed” button as shown in the following graphic:
- The code to be embedded into your HTML website looks something like this:
<iframe src="https://trinket.io/embed/python/edd948bf08" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
- Just copy and paste this code snippet into your own website.
How does it work?
Also, this prevents users from running malicious code on your web server like the following:
import os os.system("rm -rf *.*")
If you would allow the user to run code on your web server, they can easily hack your server and remove all the data (or even send it to their own server).
What other tools exist to embed code into any HTML website?
You can also use other tools. One great tool which I just discovered is repl.it. Here’s what it looks like:
You can share the code snippet or embed it using an HTML iframe. Just copy&paste the embed code into your own website. It works just like Trinket.io.
Here’s one such embeded Python interpreter:
Where to go from here?
If you have your own Python website, you better know how to write Pythonic code. There are many trolls who hate reading non-Pythonic code. They will come after you. 😉
So if you want to learn how to write Python code fast, download my Python cheat sheets. Tens of thousands of people love these cheat sheets and have already downloaded, printed, and shared them!