Skip to content

Commit 7f57979

Browse files
author
chenyumic
authored
Added sample for using Cloud Spanner with App Engine Python 3.7 runtime. (#1757)
* Added sample for using Cloud Spanner with App Engine Python 3.7 runtime. * Minor fixes. * Minor fixes.
1 parent 1ca5565 commit 7f57979

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runtime: python37
2+
3+
env_variables:
4+
SPANNER_INSTANCE: "YOUR-SPANNER-INSTANCE-ID"
5+
SPANNER_DATABASE: "YOUR-SPANNER-DATABASE-ID"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 Google LLC
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+
# [START gae_python37_cloudsql_mysql]
16+
import os
17+
18+
from flask import Flask
19+
from google.cloud import spanner
20+
21+
app = Flask(__name__)
22+
spanner_client = spanner.Client()
23+
24+
instance_id = os.environ.get('SPANNER_INSTANCE')
25+
database_id = os.environ.get('SPANNER_DATABASE')
26+
27+
28+
@app.route('/')
29+
def main():
30+
database = spanner_client.instance(instance_id).database(database_id)
31+
with database.snapshot() as snapshot:
32+
cursor = snapshot.execute_sql('SELECT 1')
33+
results = list(cursor)
34+
35+
return 'Query Result: {}'.format(results[0][0])
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2018 Google LLC
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+
16+
def test_main():
17+
import main
18+
19+
main.database_id = 'example-db'
20+
21+
main.app.testing = True
22+
client = main.app.test_client()
23+
24+
r = client.get('/')
25+
assert r.status_code == 200
26+
assert 'Query Result: 1' in r.data.decode('utf-8')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-spanner==1.6.0
2+
Flask==1.0.2

0 commit comments

Comments
 (0)