|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | + |
| 17 | +from flask import Flask |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +app = Flask(__name__) |
| 21 | + |
| 22 | + |
| 23 | +# [START numpy] |
| 24 | +@app.route('/') |
| 25 | +def calculate(): |
| 26 | + return_str = '' |
| 27 | + x = np.array([[1, 2], [3, 4]]) |
| 28 | + y = np.array([[5, 6], [7, 8]]) |
| 29 | + |
| 30 | + return_str += 'x: {} , y: {}<br />'.format(str(x), str(y)) |
| 31 | + |
| 32 | + # Multiply matrices |
| 33 | + return_str += 'x dot y : {}'.format(str(np.dot(x, y))) |
| 34 | + return return_str |
| 35 | +# [END numpy] |
| 36 | + |
| 37 | + |
| 38 | +@app.errorhandler(500) |
| 39 | +def server_error(e): |
| 40 | + logging.exception('An error occurred during a request.') |
| 41 | + return """ |
| 42 | + An internal error occurred: <pre>{}</pre> |
| 43 | + See logs for full stacktrace. |
| 44 | + """.format(e), 500 |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + # This is used when running locally. Gunicorn is used to run the |
| 48 | + # application on Google App Engine. See entrypoint in app.yaml. |
| 49 | + app.run(host='127.0.0.1', port=8080, debug=True) |
0 commit comments