def bigquery(line, cell=None): """Implements the bigquery cell magic for ipython notebooks. The supported syntax is: %%bigquery <command> [<args>] <cell> or: %bigquery <command> [<args>] Use %bigquery --help for a list of commands, or %bigquery <command> --help for help on a specific command. Args: line: the magic line. cell: the body of the notebook cell. Returns: The result of processing the magic. """ namespace = {} if line.find('$') >= 0: # We likely have variables to expand; get the appropriate context. namespace = _utils.notebook_environment() return _utils.handle_magic_line(line, cell, _bigquery_parser, namespace=namespace)
def chart(line, cell=None): """ Generate charts with Google Charts. Use %chart --help for more details. Args: line: the %chart magic line. cell: the contents of the cell. Can contain YAML chart options. """ parser = _commands.CommandParser(prog='%%chart', description=""" Generate an inline chart using Google Charts using the data in a Table, Query, dataframe, or list. Numerous types of charts are supported. Options for the charts can be specified in the cell body using YAML or JSON. """) for chart_type in ['annotation', 'area', 'bars', 'bubbles', 'calendar', 'candlestick', 'columns', 'combo', 'gauge', 'geo', 'histogram', 'line', 'map', 'org', 'paged_table', 'pie', 'sankey', 'scatter', 'stepped_area', 'table', 'timeline', 'treemap']: subparser = parser.subcommand(chart_type, 'Generate a %s chart.' % chart_type) subparser.add_argument('-f', '--fields', help='The field(s) to include in the chart') subparser.add_argument('-d', '--data', help='The name of the variable referencing the Table or Query to chart', required=True) subparser.set_defaults(chart=chart_type) parser.set_defaults(func=_chart_cell) return _utils.handle_magic_line(line, cell, parser)
def pymodule(line, cell=None): """Creates and subsequently auto-imports a python module. """ parser = _commands.CommandParser.create('pymodule') parser.add_argument('-n', '--name', help='the name of the python module to create and import') parser.set_defaults(func=_pymodule_cell) return _utils.handle_magic_line(line, cell, parser)
def extension(line): """ Load an extension. Use %extension --help for more details. """ parser = _commands.CommandParser(prog='%extension', description=""" Load an extension into Datalab. Currently only mathjax is supported. """) subparser = parser.subcommand('mathjax', 'Enabled MathJaX support in Datalab.') subparser.set_defaults(ext='mathjax') parser.set_defaults(func=_extension) return _utils.handle_magic_line(line, None, parser)
def projects(line, cell=None): parser = _commands.CommandParser.create('projects') list_parser = parser.subcommand('list', 'List available projects.') list_parser.add_argument('-f', '--filter', help='Optional wildcard id filter string used to limit the results') list_parser.set_defaults(func=_list_line, cell_prohibited=True) set_parser = parser.subcommand('set', 'Set the default project.') set_parser.add_argument('id', help='The ID of the project to use') set_parser.set_defaults(func=_set_line, cell_prohibited=True) return _utils.handle_magic_line(line, cell, parser)
def csv(line, cell=None): parser = _commands.CommandParser.create('csv') view_parser = parser.subcommand('view', 'Browse CSV files without providing a schema. ' + 'Each value is considered string type.') view_parser.add_argument('-i', '--input', help='Path of the input CSV data', required=True) view_parser.add_argument('-n', '--count', help='The number of lines to browse from head, default to 5.') view_parser.add_argument('-P', '--profile', action='store_true', default=False, help='Generate an interactive profile of the data') view_parser.set_defaults(func=_view) return _utils.handle_magic_line(line, cell, parser)
def sql(line, cell): """ Create a SQL module with one or more queries. Use %sql --help for more details. The supported syntax is: %%sql [--module <modulename>] [<optional Python code for default argument values>] [<optional named queries>] [<optional unnamed query>] At least one query should be present. Named queries should start with: DEFINE QUERY <name> on a line by itself. Args: args: the optional arguments following '%%sql'. cell: the contents of the cell; Python code for arguments followed by SQL queries. """ if cell is None: _sql_parser.print_help() else: return _utils.handle_magic_line(line, cell, _sql_parser)
def storage(line): """Implements the storage line magic for ipython notebooks. Args: line: the contents of the storage line. Returns: The results of executing the cell. """ parser = _commands.CommandParser(prog='storage', description=""" Execute various storage-related operations. Use "%storage <command> -h" for help on a specific command. """) # TODO(gram): consider adding a move command too. I did try this already using the # objects.patch API to change the object name but that fails with an error: # # Value 'newname' in content does not agree with value 'oldname'. This can happen when a value # set through a parameter is inconsistent with a value set in the request. # # This is despite 'name' being identified as writable in the storage API docs. # The alternative would be to use a copy/delete. copy_parser = parser.subcommand('copy', 'Copy one or more GCS objects to a different location.') copy_parser.add_argument('-s', '--source', help='The name of the object(s) to copy', nargs='+') copy_parser.add_argument('-d', '--destination', required=True, help='The copy destination. For multiple source items this must be a bucket.') copy_parser.set_defaults(func=_storage_copy) create_parser = parser.subcommand('create', 'Create one or more GCS buckets.') create_parser.add_argument('-p', '--project', help='The project associated with the objects') create_parser.add_argument('-b', '--bucket', help='The name of the bucket(s) to create', nargs='+') create_parser.set_defaults(func=_storage_create) delete_parser = parser.subcommand('delete', 'Delete one or more GCS buckets or objects.') delete_parser.add_argument('-b', '--bucket', nargs='*', help='The name of the bucket(s) to remove') delete_parser.add_argument('-o', '--object', nargs='*', help='The name of the object(s) to remove') delete_parser.set_defaults(func=_storage_delete) list_parser = parser.subcommand('list', 'List buckets in a project, or contents of a bucket.') list_parser.add_argument('-p', '--project', help='The project associated with the objects') group = list_parser.add_mutually_exclusive_group() group.add_argument('-o', '--object', help='The name of the objects(s) to list; can include wildchars', nargs='?') group.add_argument('-b', '--bucket', help='The name of the buckets(s) to list; can include wildchars', nargs='?') list_parser.set_defaults(func=_storage_list) read_parser = parser.subcommand('read', 'Read the contents of a storage object into a Python variable.') read_parser.add_argument('-o', '--object', help='The name of the object to read', required=True) read_parser.add_argument('-v', '--variable', required=True, help='The name of the Python variable to set') read_parser.set_defaults(func=_storage_read) view_parser = parser.subcommand('view', 'View the contents of a storage object.') view_parser.add_argument('-n', '--head', type=int, default=20, help='The number of initial lines to view') view_parser.add_argument('-t', '--tail', type=int, default=20, help='The number of lines from end to view') view_parser.add_argument('-o', '--object', help='The name of the object to view', required=True) view_parser.set_defaults(func=_storage_view) write_parser = parser.subcommand('write', 'Write the value of a Python variable to a storage object.') write_parser.add_argument('-v', '--variable', help='The name of the source Python variable', required=True) write_parser.add_argument('-o', '--object', required=True, help='The name of the destination GCS object to write') write_parser.set_defaults(func=_storage_write) return _utils.handle_magic_line(line, None, parser)