Showing posts with label tinywebdb. Show all posts
Showing posts with label tinywebdb. Show all posts

Saturday, July 16, 2011

Android App Inventor - Load CSV File to Datastore

In a previous post I described how to use the TinyWebDB component within the Android App Inventor. This post described using the Google supplied TinyWebDB web service to store data on the web.

I was recently asked a question about how to pre-load the web service with data. Google provides a bulk-uploader service that you can use to load values from a file. After doing some research into the bulk-uploader it seemed to be very complex and I started to think of other ways to achieve a similar result.

The solution I created was using the web interface provided in the web service to upload a comma-separated file to the blobstore and subsequently loading the value into the datastore. I have attached the source code of this new version of the TinyWebDB web service to my source code page.

The main visible change to the application is the addition of a page to handle the uploads of the CSV files to the App Engine Blobstore. This uses a standard File Upload form control to display the File Open Dialog.

The page also displays the uploaded files along with an option to load the contents of the CSV file to the App Engine Datastore.


The main logic of the Blobstore Upload was courtesy of User: 010110110101 on StackOverflow who provided a great example of using Blobstore to store files in App Engine. I added this logic to the TinyWebDB web service code.

With the file uploaded to the blobstore, the web service then uses the Python CSV reader to read the file and load each key/value pair into the datastore. These key/value pairs are then available to the AppInventor application through the standard operations.

Feel free to use/modify this code for your own purposes. Enjoy.

My other App Inventor tutorials are:
App Inventor - Simple SMS
App Inventor - Location Services
App Inventor - Lists
App Inventor - TinyWebDB

Links
TinyWebDB Web Service
Blobstore Python Code - StackOverflow
CSV Reader in AppEngine - StackOverflow

Tuesday, April 5, 2011

Android App Inventor - TinyWebDB

The next post in my series on Android App Inventor makes use of one the 'Not for Primetime' components. The TinyWebDB component allows you to store data on a web service. This means that the data can be accessed by the Android phone as well as a web application.

For testing purposes Google have provided a test web service at http://appinvtinywebdb.appspot.com/.  This service is a limited service and will only store 1000 entries. Due to these limitations it should NOT be used for your applications. Google provides instructions on how to create your own web service using Python and the Google App Engine.

The following example uses the application I built for the Android App Inventor - Lists post. This post will show how to store the contents of a list to the TinyWebDB web service and also retrieve the contents of a list from the TinyWebDB web service.


To begin we need add two buttons (btnStoretoWeb & btnGetListfromWeb).



We also need to add the TinyWebDB component. I have also added a Notifier component to allow us to return a message to the user when the data is stored.

 The TinyWebDB1 control needs to have the ServiceURL set to the debug URL: http://appinvtinywebdb.appspot.com/


Once the GUI has been configured we start the block editor and create the logic to save the list to the TinyWebDB.

To store a value on the web service we add the btnStoretoWeb.Click event. Within the event we call the TinyWebDB1.StoreValue method. This method has two parameters. The 'tag' parameter allows you to assign a key to the data. The second parameter is the list you are going to store.

The second event shown above is the TinyWebDB1.ValueStored event. When a value is stored in the TinyWebDB this event is triggered. I have used it to provide a notification to the user that it worked.

The next step is to retrieve the values from the web service and repopulate the list. In this case I have used a button to manually trigger the load of the list. In a production application you may want to put this code into the Screen.Initialize event.

In the btnGetFromWeb.Click event we call the TinyWebDB1.GetValue method with the name of the 'tag' we want to retrieve. In this case it is the MyStoredList tag.

Once the GetValue call is sent we need to wait for the TinyWebDB1.GotValue event to be called. This ensures that our application can remain responsive even if the call to the web service is slow to respond. When the event is triggered the application has access to the parameter valuefromWebDB. This contains the list we stored previously.

We can then store this value into the list. We also update the label to show the number of items in the list. I also load the ListPicker1 component.

I hope this tutorial has been helpful. Please feel free to comment if you need further information.

I have added a link to the App Inventor source here.

My other App Inventor tutorials are:
App Inventor - Simple SMS
App Inventor - Location Services
App Inventor - Lists