| Download
Github repo cloud-examples: https://github.com/sagemath/cloud-examples
Project: Support and Testing
Views: 7963License: MIT
#!/usr/bin/env python1# -*- coding: utf-8 -*-2# This runs a demo-webservice inside SMC.3# Run by typing4# sage -python main.py56import json7import os89from flask import Flask, request10app = Flask(__name__)11#app.debug = True # Uncomment this if you wish to debug1213port = 876514info = json.load(15open(os.path.join(os.environ['HOME'], ".smc", "info.json"), 'r'))16base_url = "/%s/port/%s" % (info['project_id'], port)1718html_template = """19<!DOCTYPE html20PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"21"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">22<html>2324<head>25<title>%(title)s</title>26</head>2728<body>2930<p>%(content)s</p>3132</body>33</html>34"""353637@app.route(base_url + '/', strict_slashes=False)38def info():39content = """<p>SMC Webservice running</p>40<p><b>Routes:</b></p>4142<ul>43<li><a href="%(route)s/">%(route)s/ - list of routes</a></li>44<li><a href="%(route)s/get">%(route)s/get - simple get request</a></li>45</ul>""" % {'route': base_url}46return html_template % {'title': 'Info', 'content': content}474849@app.route(base_url + '/get', methods=['GET'])50def get_something():51content = """<p>Try something like <a href="%(base_url)s/get?something=foo">%(base_url)s/get?something=foo</a></p>""" % {52'base_url': base_url}53if request.method == 'GET':54something = request.args.get("something", "")55if len(something) > 0:56content = "Got " + something57return html_template % {'title': 'Info', 'content': content}5859if __name__ == "__main__":60try:61info = json.load(62open(os.path.join(os.environ['HOME'], ".smc", "info.json"), 'r'))63print("Try to open https://cloud.sagemath.com" + base_url + '/')64app.run(host='0.0.0.0', port=port)65import sys66sys.exit(0)67except Exception as e:68print "... failed, try another port (change the port= line in the script) \n%s" % e697071