diff --git a/datalab/bigquery/commands/_bigquery.py b/datalab/bigquery/commands/_bigquery.py index 032f1e56c..be8d72c17 100644 --- a/datalab/bigquery/commands/_bigquery.py +++ b/datalab/bigquery/commands/_bigquery.py @@ -944,7 +944,7 @@ def _table_viewer(table, rows_per_page=25, fields=None): rowNumberCell: 'gchart-table-rownumcell' }} }}, - {{source_index: {source_index}, fields: '{fields}'}}, + {{source_index: {source_index}, fields: '{fields}', legacy: 'true'}}, 0, {total_rows}); }} diff --git a/datalab/utils/commands/_chart_data.py b/datalab/utils/commands/_chart_data.py index f9601f392..f329dc7c9 100644 --- a/datalab/utils/commands/_chart_data.py +++ b/datalab/utils/commands/_chart_data.py @@ -30,7 +30,9 @@ from . import _utils -@IPython.core.magic.register_cell_magic +# Disable the magic here because another one with same name is available under +# google.datalab namespace. +# @IPython.core.magic.register_cell_magic def _get_chart_data(line, cell_body=''): refresh = 0 diff --git a/google/datalab/bigquery/_table.py b/google/datalab/bigquery/_table.py index 07424cd86..9566252ba 100644 --- a/google/datalab/bigquery/_table.py +++ b/google/datalab/bigquery/_table.py @@ -139,6 +139,11 @@ def name(self): """The TableName named tuple (project_id, dataset_id, table_id, decorator) for the table.""" return self._name_parts + @property + def full_name(self): + """The full name of the table in the form of project.dataset.table.""" + return self._full_name + @property def job(self): """ For tables resulting from executing queries, the job that created the table. diff --git a/google/datalab/bigquery/commands/_bigquery.py b/google/datalab/bigquery/commands/_bigquery.py index e9c6c8b29..d70d62b12 100644 --- a/google/datalab/bigquery/commands/_bigquery.py +++ b/google/datalab/bigquery/commands/_bigquery.py @@ -736,7 +736,7 @@ def _table_cell(args, cell_body): tables = [] for dataset in datasets: - tables.extend([str(table) for table in dataset if fnmatch.fnmatch(str(table), filter_)]) + tables.extend([table.full_name for table in dataset if fnmatch.fnmatch(table.full_name, filter_)]) return _render_list(tables) @@ -992,7 +992,7 @@ def _table_viewer(table, rows_per_page=25, fields=None): # TODO(gram): rework this to use google.datalab.utils.commands.chart_html if not table.exists(): - raise Exception('Table %s does not exist' % str(table)) + raise Exception('Table %s does not exist' % table.full_name) _HTML_TEMPLATE = u"""
{static_table}
@@ -1047,7 +1047,7 @@ def _table_viewer(table, rows_per_page=25, fields=None): fields = google.datalab.utils.commands.get_field_list(fields, table.schema) div_id = google.datalab.utils.commands.Html.next_id() meta_count = ('rows: %d' % table.length) if table.length >= 0 else '' - meta_name = str(table) if table.job is None else ('job: %s' % table.job.id) + meta_name = table.full_name if table.job is None else ('job: %s' % table.job.id) if table.job: if table.job.cache_hit: meta_cost = 'cached' @@ -1076,7 +1076,7 @@ def _table_viewer(table, rows_per_page=25, fields=None): static_table=google.datalab.utils.commands.HtmlBuilder.render_chart_data(data), meta_data=meta_data, chart_style=chart, - source_index=google.datalab.utils.commands.get_data_source_index(str(table)), + source_index=google.datalab.utils.commands.get_data_source_index(table.full_name), fields=','.join(fields), total_rows=total_count, rows_per_page=rows_per_page, diff --git a/google/datalab/utils/commands/_chart_data.py b/google/datalab/utils/commands/_chart_data.py index 4034c0b6a..a8fd7667c 100644 --- a/google/datalab/utils/commands/_chart_data.py +++ b/google/datalab/utils/commands/_chart_data.py @@ -24,6 +24,7 @@ import json +import datalab.utils.commands import google.datalab.data import google.datalab.utils @@ -41,17 +42,30 @@ def _get_chart_data(line, cell_body=''): fields = metadata.get('fields', '*') first_row = int(metadata.get('first', 0)) count = int(metadata.get('count', -1)) + legacy = metadata.get('legacy', None) + + # Both legacy and non-legacy table viewer calls this magic for new pages of data. + # Need to find their own data source --- one under datalab.utils.commands._utils + # and the other under google.datalab.utils.commands._utils. + if legacy is not None: + data_source = datalab.utils.commands._utils._data_sources + else: + data_source = _utils._data_sources source_index = int(source_index) - if source_index >= len(_utils._data_sources): # Can happen after e.g. kernel restart + if source_index >= len(data_source): # Can happen after e.g. kernel restart # TODO(gram): get kernel restart events in charting.js and disable any refresh timers. print('No source %d' % source_index) return IPython.core.display.JSON({'data': {}}) - source = _utils._data_sources[source_index] + source = data_source[source_index] schema = None controls = metadata['controls'] if 'controls' in metadata else {} - data, _ = _utils.get_data(source, fields, controls, first_row, count, schema) + if legacy is not None: + data, _ = datalab.utils.commands.get_data( + source, fields, controls, first_row, count, schema) + else: + data, _ = _utils.get_data(source, fields, controls, first_row, count, schema) except Exception as e: google.datalab.utils.print_exception_with_last_stack(e) print('Failed with exception %s' % e)