Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8cfbe2

Browse files
authored
Merge pull request GoogleCloudPlatform#474 from GoogleCloudPlatform/scipy2
Add Scipy example
2 parents 607f9e3 + 7204d54 commit c8cfbe2

File tree

10 files changed

+103
-0
lines changed

10 files changed

+103
-0
lines changed

appengine/flexible/scipy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assets/resized_google_logo.jpg

appengine/flexible/scipy/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SciPy on App Engine Flexible
2+
3+
This sample demonstrates how to use SciPy to resize an image on App Engine Flexible.
4+

appengine/flexible/scipy/app.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
runtime: python
2+
vm: true
3+
entrypoint: gunicorn -b :$PORT main:app
4+
5+
runtime_config:
6+
python_version: 3
21.1 KB
Loading
Loading

appengine/flexible/scipy/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
import os
17+
18+
from flask import Flask
19+
import scipy.misc
20+
21+
app = Flask(__name__)
22+
23+
24+
# [START scipy]
25+
@app.route('/')
26+
def resize():
27+
"""Demonstrates using scipy to resize an image."""
28+
app_path = os.path.dirname(os.path.realpath(__file__))
29+
image_path = os.path.join(app_path, 'assets/google_logo.jpg')
30+
img = scipy.misc.imread(image_path)
31+
img_tinted = scipy.misc.imresize(img, (300, 300))
32+
output_image_path = os.path.join(
33+
app_path, 'assets/resized_google_logo.jpg')
34+
# Write the tinted image back to disk
35+
scipy.misc.imsave(output_image_path, img_tinted)
36+
return "Image resized."
37+
# [END scipy]
38+
39+
40+
@app.errorhandler(500)
41+
def server_error(e):
42+
logging.exception('An error occurred during a request.')
43+
return """
44+
An internal error occurred: <pre>{}</pre>
45+
See logs for full stacktrace.
46+
""".format(e), 500
47+
48+
49+
if __name__ == '__main__':
50+
# This is used when running locally. Gunicorn is used to run the
51+
# application on Google App Engine. See entrypoint in app.yaml.
52+
app.run(host='127.0.0.1', port=8080, debug=True)

appengine/flexible/scipy/main_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 filecmp
16+
import os
17+
18+
import main
19+
20+
21+
def test_index():
22+
main.app.testing = True
23+
client = main.app.test_client()
24+
test_path = os.path.dirname(os.path.realpath(__file__))
25+
asset_path = os.path.join(
26+
test_path, 'assets/resized_google_logo.jpg')
27+
fixtured_path = os.path.join(
28+
test_path, 'fixtures/assets/resized_google_logo.jpg')
29+
try:
30+
os.remove(asset_path)
31+
except OSError:
32+
pass # if doesn't exist
33+
r = client.get('/')
34+
assert filecmp.cmp(asset_path, fixtured_path)
35+
assert r.status_code == 200
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==0.11.1
2+
gunicorn==19.6.0
3+
scipy==0.18.0
4+
Pillow==3.3.1

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ requests==2.11.1
4040
requests[security]==2.11.1
4141
responses==0.5.1
4242
rsa==3.4.2
43+
scipy==0.18.0
4344
sendgrid==3.2.10
4445
simplejson==3.8.2
4546
six==1.10.0

0 commit comments

Comments
 (0)