0

I'm looking to use the %storage magic in a Datalab notebook, but within a function. The line looks like:

%storage write --variable df --object $bucket_object

and when calling the function I get this error:

Undefined variable referenced in command line: $bucket_object

bucket_object is defined previously in the function, and this works when running outside a function.

This answer regarding a similar question for iPython recommends finding the path and then importing like:

from IPython.core.magics.display import Javascript

Is there a similar approach to call a Datalab magic's respective function so that it would work within another function?

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132

1 Answers1

3

The %storage and other similar commands are mostly syntactic sugar over underlying APIs.

For example, in this case google.datalab.storage APIs. Documentation is at http://googledatalab.github.io/pydatalab/google.datalab.storage.html

Something like this might work:

import google.datalab.storage as storage

bucket = storage.Bucket(bucket_name)
obj = bucket.object(object_key)
obj.write_stream(data, content_type)

Hope that helps.

Nikhil Kothari
  • 5,215
  • 2
  • 22
  • 28
  • `write_stream` gives this error: "TypeError: write_stream() takes exactly 3 arguments (2 given)". Based on the [documentation](http://googledatalab.github.io/pydatalab/_modules/google/datalab/storage/_object.html) it looked like `upload` might work instead, but that gave this error: "ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." – Max Ghenis Nov 01 '17 at 00:24
  • Note that write_stream also takes content type, so it should be obj.write_stream(data, content_type). – Nikhil Kothari Nov 09 '17 at 15:32