Spaces:
Sleeping
Sleeping
import flask | |
import os | |
import mypkg | |
from mypkg import draw | |
from mypkg.convert import Convert | |
app = flask.Flask(__name__, template_folder="./templates/") | |
def index(): | |
print('Route: /') | |
return flask.render_template('index.html', mypkg_version=mypkg.__version__) | |
def about(): | |
print('Route: /about') | |
return flask.render_template('about.html') | |
def user(name: str): | |
print(f'Route: /user/{name}') | |
return flask.render_template('user-name.html', name=name) | |
def evaluate(expression: str): | |
print(f'Route: /eval/{expression}') | |
# You can do some processing here before rendering the template. | |
result = eval(expression) | |
print(f"\tResult: {result}") | |
return flask.render_template('evaluate-expression.html', expression=expression, result=result) | |
def show_circle(): | |
img = draw.circle(width=500, height=500) | |
image = Convert.cv_to_base64(img) | |
return flask.render_template('show-image.html', image=image, imageLabel='Circle') | |
def show_shapes(): | |
return flask.render_template('show-shapes.html') | |
def draw_target(): | |
""" | |
Try testing with: | |
curl -X GET "localhost:5000/draw?target=blank" | |
curl -X GET "localhost:5000/draw?target=circle" | |
curl -X GET "localhost:5000/draw?target=rectangle" | |
""" | |
# TODO: Call this from javascript for toggling between images. | |
target = flask.request.args.get('target', default='blank', type=str) | |
width = flask.request.args.get('width', default=500, type=int) | |
height = flask.request.args.get('height', default=500, type=int) | |
if target == 'blank': | |
img = draw.blank(width=width, height=height) | |
elif target == 'circle': | |
img = draw.circle(width=width, height=height) | |
elif target == 'rectangle': | |
img = draw.rectangle(width=width, height=height) | |
else: | |
raise ValueError(f'Unsupported target: {target}') | |
image = Convert.cv_to_base64(img) | |
return flask.jsonify( | |
isError=False, | |
message='Success', | |
statusCode=200, | |
data=image | |
) | |
def echo(): | |
""" | |
Try testing with: | |
curl -X GET "localhost:5000/echo?a=hello&b=world" | |
curl -X POST "localhost:5000/echo?a=hello&b=world" | |
""" | |
msg = 'Echo:' | |
for key, val in flask.request.args.to_dict().items(): | |
msg += f'\n\t{key}={val}' | |
if flask.request.method == 'GET': | |
return flask.jsonify( | |
isError=False, | |
message='Success', | |
statusCode=200, | |
data=msg | |
) | |
else: | |
print(msg) | |
return flask.jsonify( | |
isError=False, | |
message='Success', | |
statusCode=200, | |
# data=msg | |
) | |
def debug_scrolling(): | |
return flask.render_template('scroll_debug.html') | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) | |