Spaces:
Sleeping
Sleeping
File size: 3,124 Bytes
f418104 d7d22f5 c9a11d0 f418104 d7d22f5 f418104 c9a11d0 f418104 404baf7 f418104 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import flask
import os
import mypkg
from mypkg import draw
from mypkg.convert import Convert
app = flask.Flask(__name__, template_folder="./templates/")
@app.route('/')
def index():
print('Route: /')
return flask.render_template('index.html', mypkg_version=mypkg.__version__)
@app.route('/about')
def about():
print('Route: /about')
return flask.render_template('about.html')
@app.route('/user/<name>')
def user(name: str):
print(f'Route: /user/{name}')
return flask.render_template('user-name.html', name=name)
@app.route('/evaluate/<expression>')
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)
@app.route('/show/circle')
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')
@app.route('/show/shapes')
def show_shapes():
return flask.render_template('show-shapes.html')
@app.route('/draw', methods=['GET'])
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
)
@app.route('/echo', methods=['GET', 'POST'])
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
)
@app.route('/debug/scrolling')
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)))
|