Run Python code in a browser without a server
Python is a great language to code in — it’s concise, versatile, all-purpose and has a thriving community. However, even with all of its popularity, Python hasn’t really found its way into the sphere of front-end web development. Since it is heavily used on server and backend environments, often times there are many nice snippets of general purpose code that would be great to bring into a front-end environment.
In order to execute our Python code, we’ll need to create a pipe in Flex.io. You’ll need to sign in to your Flex.io account, or you can sign up for a free account to get started. Once you’re signed in, you’ll need to create a new pipe.
We set up a very simple pipe using Flex.io that has only one step.
Add Execute Step
This step adds a code editor to the pipe where you can add Python code to be executed when the pipe is run. To do this, we simply use the
executecommand and save the step:
execute lang: python
Add Python Code
Once you save the step, the code editor will be displayed where you can edit your Python code. Add the following code and save the step:
writer = context.output.create(name='Hello')
if 'message' in context.input.env:
Here’s the very basic HTML form and output markup we created (if you’re wondering about the classes on the elements, we’re using the Tachyons CSS toolkit):
<form class="flex flex-row">
To call this pipe from your code, all that is needed to run your Python code is a simple AJAX call to Flex.io:
Using the Flex.io API to do the heavy lifting of actually executing the python code is simple and allows you to offload your Python scripts to the cloud for use in your web apps through a single call to the Flex.io API.
We hope you’ve enjoyed this quick example of the Flex.io API and find it useful.