forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added samples from datastore-samples
- Loading branch information
Showing
11 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
## NDB Overview Sample | ||
|
||
This is a sample app for Google App Engine that exercises the [NDB Python API](https://cloud.google.com/appengine/docs/python/ndb/). | ||
|
||
See our other [Google Cloud Platform github | ||
repos](https://github.com/GoogleCloudPlatform) for sample applications and | ||
scaffolding for other python frameworks and use cases. | ||
|
||
## Run Locally | ||
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app). | ||
2. Setup the gcloud tool. | ||
|
||
``` | ||
gcloud components update app | ||
gcloud auth login | ||
gcloud config set project <your-app-id> | ||
``` | ||
You don't need a valid app-id to run locally, but will need a valid id to deploy below. | ||
|
||
1. Clone this repo. | ||
|
||
``` | ||
git clone https://github.com/GoogleCloudPlatform/datastore-pthon-samples.git | ||
cd datastore-python-samples/ndb-overview | ||
``` | ||
1. Run this project locally from the command line. | ||
|
||
``` | ||
gcloud preview app run ./ | ||
``` | ||
|
||
1. Visit the application at [http://localhost:8080](http://localhost:8080). | ||
|
||
## Deploying | ||
|
||
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical) | ||
2. Configure gcloud with your app id. | ||
|
||
``` | ||
gcloud config set project <your-app-id> | ||
``` | ||
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks. | ||
1. Use gcloud to deploy your app. | ||
|
||
``` | ||
gcloud preview app deploy ./ | ||
``` | ||
|
||
1. Congratulations! Your application is now live at your-app-id.appspot.com | ||
|
||
## Contributing changes | ||
|
||
* See [CONTRIBUTING.md](../../CONTRIBUTING.md) | ||
|
||
## Licensing | ||
|
||
* See [LICENSE](../../LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This file specifies your Python application's runtime configuration | ||
# including URL routing, versions, static file uploads, etc. See | ||
# https://developers.google.com/appengine/docs/python/config/appconfig | ||
# for details. | ||
|
||
version: 1 | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: yes | ||
|
||
# Handlers define how to route requests to your application. | ||
handlers: | ||
|
||
# This handler tells app engine how to route requests to a WSGI application. | ||
# The script value is in the format <path.to.module>.<wsgi_application> | ||
# where <wsgi_application> is a WSGI application object. | ||
- url: .* # This regex directs all routes to main.app | ||
script: main.app | ||
|
||
libraries: | ||
- name: webapp2 | ||
version: "2.5.2" |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
indexes: | ||
|
||
# AUTOGENERATED | ||
|
||
# This index.yaml is automatically updated whenever the dev_appserver | ||
# detects that a new type of query is run. If you want to manage the | ||
# index.yaml file manually, remove the above marker line (the line | ||
# saying "# AUTOGENERATED"). If you want to manage some indexes | ||
# manually, move them above the marker line. The index.yaml file is | ||
# automatically uploaded to the admin console when you next deploy | ||
# your application using appcfg.py. | ||
|
||
- kind: Greeting | ||
ancestor: yes | ||
properties: | ||
- name: date | ||
direction: desc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START all] | ||
import cgi | ||
import urllib | ||
|
||
import webapp2 | ||
|
||
from google.appengine.ext import ndb | ||
|
||
|
||
# [START greeting] | ||
class Greeting(ndb.Model): | ||
"""Models an individual Guestbook entry with content and date.""" | ||
content = ndb.StringProperty() | ||
date = ndb.DateTimeProperty(auto_now_add=True) | ||
# [END greeting] | ||
|
||
# [START query] | ||
@classmethod | ||
def query_book(cls, ancestor_key): | ||
return cls.query(ancestor=ancestor_key).order(-cls.date) | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
def get(self): | ||
self.response.out.write('<html><body>') | ||
guestbook_name = self.request.get('guestbook_name') | ||
ancestor_key = ndb.Key("Book", guestbook_name or "*notitle*") | ||
greetings = Greeting.query_book(ancestor_key).fetch(20) | ||
|
||
for greeting in greetings: | ||
self.response.out.write('<blockquote>%s</blockquote>' % | ||
cgi.escape(greeting.content)) | ||
# [END query] | ||
|
||
self.response.out.write(""" | ||
<form action="/sign?%s" method="post"> | ||
<div><textarea name="content" rows="3" cols="60"></textarea></div> | ||
<div><input type="submit" value="Sign Guestbook"></div> | ||
</form> | ||
<hr> | ||
<form>Guestbook name: <input value="%s" name="guestbook_name"> | ||
<input type="submit" value="switch"></form> | ||
</body> | ||
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}), | ||
cgi.escape(guestbook_name))) | ||
|
||
|
||
# [START submit] | ||
class SubmitForm(webapp2.RequestHandler): | ||
def post(self): | ||
# We set the parent key on each 'Greeting' to ensure each guestbook's | ||
# greetings are in the same entity group. | ||
guestbook_name = self.request.get('guestbook_name') | ||
greeting = Greeting(parent=ndb.Key("Book", guestbook_name or "*notitle*"), | ||
content=self.request.get('content')) | ||
greeting.put() | ||
# [END submit] | ||
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name})) | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', MainPage), | ||
('/sign', SubmitForm) | ||
]) | ||
# [END all] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
## NDB Transactions Sample | ||
|
||
This is a sample app for Google App Engine that exercises the [NDB Transactions Python API](https://cloud-dot-devsite.googleplex.com/appengine/docs/python/ndb/transactions) | ||
|
||
This app presents a list of notes. After you submit a note with a particular title, you may not change that note or submit a new note with the same title. There are multiple note pages available. | ||
|
||
See our other [Google Cloud Platform github | ||
repos](https://github.com/GoogleCloudPlatform) for sample applications and | ||
scaffolding for other python frameworks and use cases. | ||
|
||
## Run Locally | ||
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app). | ||
2. Setup the gcloud tool. | ||
|
||
``` | ||
gcloud components update app | ||
gcloud auth login | ||
gcloud config set project <your-app-id> | ||
``` | ||
You don't need a valid app-id to run locally, but will need a valid id to deploy below. | ||
|
||
1. Clone this repo. | ||
|
||
``` | ||
git clone https://github.com/GoogleCloudPlatform/datastore-samples.git | ||
cd datastore-samples/python/ndb/transactions | ||
``` | ||
1. Run this project locally from the command line. | ||
|
||
``` | ||
pip install -r requirements.txt -t lib/ | ||
gcloud preview app run ./app.yaml | ||
``` | ||
|
||
1. Visit the application at [http://localhost:8080](http://localhost:8080). | ||
|
||
## Deploying | ||
|
||
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical) | ||
2. Configure gcloud with your app id. | ||
|
||
``` | ||
gcloud config set project <your-app-id> | ||
``` | ||
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks. | ||
1. Use gcloud to deploy your app. | ||
|
||
``` | ||
pip install -r requirements.txt -t lib/ | ||
gcloud preview app deploy ./app.yaml | ||
``` | ||
|
||
1. Congratulations! Your application is now live at your-app-id.appspot.com | ||
|
||
## Contributing changes | ||
|
||
* See [CONTRIBUTING.md](../../../CONTRIBUTING.md) | ||
|
||
## Licensing | ||
|
||
* See [LICENSE](../../../LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file specifies your Python application's runtime configuration | ||
# including URL routing, versions, static file uploads, etc. See | ||
# https://developers.google.com/appengine/docs/python/config/appconfig | ||
# for details. | ||
|
||
version: 1 | ||
runtime: python27 | ||
api_version: 1 | ||
threadsafe: yes | ||
|
||
# Handlers define how to route requests to your application. | ||
handlers: | ||
|
||
# This handler tells app engine how to route requests to a WSGI application. | ||
# The script value is in the format <path.to.module>.<wsgi_application> | ||
# where <wsgi_application> is a WSGI application object. | ||
- url: .* # This regex directs all routes to main.app | ||
script: main.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
`appengine_config.py` is automatically loaded when Google App Engine | ||
starts a new instance of your application. This runs before any | ||
WSGI applications specified in app.yaml are loaded. | ||
""" | ||
|
||
from google.appengine.ext import vendor | ||
|
||
# Third-party libraries are stored in "lib", vendoring will make | ||
# sure that they are importable by the application. | ||
vendor.add('lib') |
Binary file not shown.
Oops, something went wrong.