{% extends "scrypture_base.html" %} {% block scrypture_content %}
Scrypture makes it easy to put Python scripts online. Simply add a class to your Python script and Scrypture will automatically serve your script through the web interface and API.
Here's how easy it is:
"""b64encode.py: encode or decode base64 strings"""
import base64
def b64encode(s):
return base64.encodestring(s)
def b64decode(s):
return repr(str(base64.decodestring(s)))
### WebAPI ###
from scrypture import webapi
class WebAPI(webapi.WebAPI):
input_text = webapi.text_input('Input')
encode_or_decode = webapi.radio_field('What do you want to do?',
choices=[('encode', 'encode'),
('decode', "decode")],
default='encode')
submit_button = webapi.submit_button('Convert')
def run(self, form_input):
input_text = form_input['input_text']
encode_or_decode = form_input['encode_or_decode']
if encode_or_decode == 'encode':
output = b64encode(input_text)
else:
output = b64decode(input_text)
return {'output_type' : 'simple',
'output' : output}
This script can be accessed in the usual ways:
~$ curl http://www.scrypture.net/api/v1/b64encode \
-X GET --data "encode_or_decode=\"encode\"&input_text=\"Hello, World\""
{"output_type": "simple", "output": "SGVsbG8sIFdvcmxk\n"}
Or through the auto-generated Python API:
>>> import scrypture_api
>>> s = scrypture_api.ScryptureAPI()
>>> print s.b64encode(encode_or_decode='encode', input_text="Hello, World")
{u'output': u'SGVsbG8sIFdvcmxk\n', u'output_type': u'simple'}
And of course, through the web interface: