google-app-engineapp-engine-ndbgoogle-app-engine-python

Equivalent of remote_api_shell.py for cloud ndb?


For Python 2 GAE, remote_api_shell.py was super handy for making updates to live datastore.

With Python 3 GAE and cloud ndb, this doesn't work. Is there a good replacement?


Solution

  • Since you are no longer in a sandbox with Python 3 GAE, you can do this, but the cloud ndb context manager makes it tedious.

    This script (run with python -i) makes it much more convenient:

    import atexit
    from google.cloud import ndb
    
    datastore_client = ndb.Client('my-app')    
    datastore_client = ndb.Client()
    ctx = datastore_client.context()
    ctx.__enter__()
    
    def close_ctx():
        ctx.__exit__(None, None, None)
    
    atexit.register(close_ctx)
    

    This script will start the ndb context manager and then present the Python interpreter for interactive use. All interactive commands you enter will be executed inside the context.

    Not sure if it is needed, but for completeness, it will also close the context manager when you ctrl-d.

    When you run the script, you'll see some warnings from Google suggesting that you are not authenticated, but you can ignore those warnings.