javascript - How to read and write files with Appengine in Python? -


i'm new @ appengine , need help.

the javascript want display generates graph.

import webapp2  main_page_html1 = """\ <html> <body> <script>   #my script comes here   var graph = new graph();   graph.addnodes('a', 'b');   graph.addedges(['a', 'b']);   #... </script> </body> </html> """ class mainpage(webapp2.requesthandler):     def get(self):         self.response.write(main_page_html1)   app = webapp2.wsgiapplication([     ('/', mainpage), ], debug=true) 

my idea store main html in file , read if requesthandler called , modify when post new graph elements client. can't this, because appengine doesn't allow standard file operations.

what easiest way make work?

app engine lets read files fine, not write them. among many alternatives plain files can use if need read/write functionality, best 2 usually: (a) app engine data store, "files" of modest moderate size; (b) google cloud storage, "files" can potentially quite large.

your use case seems call former -- i.e, datastore -- i'll focus on possibility.

define model class entities representing html want send in response -- such "models" best kept in separate model.py file, imported others of python files, that's issue of proper code organization, not of functionality. latter, whatever file place in, code like:

from google.appengine.ext import ndb  class page(ndb.model):     name = ndb.stringproperty()     html = ndb.textproperty() 

when need page name, code like:

page = page.query(page.name == the_name).get() if page none:     page = page(name=the_name, html=main_page_html1)     page.put() 

and set new, modified html content on existing page page fetched, just

page.html = new_html_content page.put() 

the put calls return key may want save (for example in memcache) if want "strong consistency" (since key.get() guaranteed fetch latest updated content, while getting query, without other precautions, might saved version of data -- exhibits eventual consistency, not "immediate" updates).

but it's hard more specific in offering advice how should best use datastore without knowing much, more exact requirements -- how determine page display and/or update (that given name property in example code, while have been given filename if have ordinary read/write files wished), consistency (immediacy of updates) requirements, , forth.

(for use cases 1 infer incomplete specs, i'd use name, here have modeled property, instead id, part of key -- but, i'm trying keep things simple match little have expressed specs).

note in approach whole html content re-written each time want change -- same goes main alternative (suggested potentially larger files), google cloud storage: no actual "incremental updates", complete re-writes affect change "file"'s contents.

that's main difference between gcs , common filesystem (while datastore offers more functionality on top, such queries , ordering of entities -- we're not using of functionality here because you're asking filesystem-like behavior).


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -