Beispiel #1
0
def _table_viewer(table, rows_per_page=25, fields=None):
  """  Return a table viewer.

    This includes a static rendering of the first page of the table, that gets replaced
    by the charting code in environments where Javascript is executable and BQ is available.

  Args:
    table: the table to view.
    rows_per_page: how many rows to display at one time.
    fields: an array of field names to display; default is None which uses the full schema.
  Returns:
    A string containing the HTML for the table viewer.
  """
  if not table.exists():
    raise Exception('Table %s does not exist' % str(table))

  _HTML_TEMPLATE = u"""
    <div class="bqtv" id="{div_id}">{static_table}</div>
    <br />{meta_data}<br />
    <script>
      require(['extensions/charting', 'element!{div_id}', 'style!/static/extensions/charting.css'],
        function(charts, dom) {{
          charts.render(dom,
            {{
              chartStyle:"{chart_style}",
              dataName:"{data_name}",
              fields:"{fields}",
              totalRows:{total_rows},
              rowsPerPage:{rows_per_page},
            }}, {{}}, {data});
        }}
      );
    </script>
  """

  if fields is None:
    fields = _utils.get_field_list(fields, table.schema)
  div_id = _html.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)
  if table.job:
    if table.job.cache_hit:
      meta_cost = 'cached'
    else:
      bytes = gcp.bigquery._query_stats.QueryStats._size_formatter(table.job.bytes_processed)
      meta_cost = '%s processed' % bytes
    meta_time = 'time: %.1fs' % table.job.total_time
  else:
    meta_cost = ''
    meta_time = ''

  data, total_count = _utils.get_data(table, fields, 0, rows_per_page)

  if total_count < 0:
    # The table doesn't have a length metadata property but may still be small if we fetched less
    # rows than we asked for.
    fetched_count = len(data['rows'])
    if fetched_count < rows_per_page:
      total_count = fetched_count

  chart = 'table' if 0 <= total_count <= rows_per_page else 'paged_table'
  meta_entries = [meta_count, meta_time, meta_cost, meta_name]
  meta_data = '(%s)' % (', '.join([entry for entry in meta_entries if len(entry)]))

  return _HTML_TEMPLATE.format(div_id=div_id,
                               static_table=_html.HtmlBuilder.render_chart_data(data),
                               meta_data=meta_data,
                               chart_style=chart,
                               data_name=_utils.get_data_source_index(str(table)),
                               fields=','.join(fields),
                               total_rows=total_count,
                               rows_per_page=rows_per_page,
                               data=json.dumps(data, cls=gcp._util.JSONEncoder))
Beispiel #2
0
def _table_viewer(table, rows_per_page=25, fields=None):
  """  Return a table viewer.

  Args:
    table: the table to view.
    rows_per_page: how many rows to display at one time.
    fields: an array of field names to display; default is None which uses the full schema.
  Returns:
    A string containing the HTML for the table viewer.
  """
  if not table.exists():
    raise Exception('%s does not exist' % str(table))

  _HTML_TEMPLATE = """
    <div class="bqtv" id="%s"></div>
    <br />%s<br />
    <script>
      require(['extensions/charting', 'element!%s', 'style!/static/extensions/charting.css'],
        function(charts, dom) {
          charts.render(dom,
            {
              chartStyle:"%s",
              dataName:"%s",
              fields:"%s",
              totalRows:%d,
              rowsPerPage:%d,
            }, {}, %s);
        }
      );
    </script>
  """

  if fields is None:
    fields = _utils.get_field_list(fields, table.schema)
  div_id = _html.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)
  if table.job:
    if table.job.cache_hit:
      meta_cost = 'cached'
    else:
      bytes = gcp.bigquery._query_stats.QueryStats._size_formatter(table.job.bytes_processed)
      meta_cost = '%s processed' % bytes
    meta_time = 'time: %.1fs' % table.job.total_time
  else:
    meta_cost = ''
    meta_time = ''

  data, total_count = _utils.get_data(table, fields, 0, rows_per_page)

  if total_count < 0:
    # The table doesn't have a length metadata property but may still be small if we fetched less
    # rows than we asked for.
    fetched_count = len(data['rows'])
    if fetched_count < rows_per_page:
      total_count = fetched_count

  chart = 'table' if 0 <= total_count <= rows_per_page else 'paged_table'
  meta_entries = [meta_count, meta_time, meta_cost, meta_name]
  meta_data = '(%s)' % (', '.join([entry for entry in meta_entries if len(entry)]))

  return _HTML_TEMPLATE %\
      (div_id, meta_data, div_id, chart,
       _utils.get_data_source_index(str(table)), ','.join(fields), total_count, rows_per_page,
       json.dumps(data, cls=gcp._util.JSONEncoder))
Beispiel #3
0
def _table_viewer(table, rows_per_page=25, fields=None):
    """  Return a table viewer.

  Args:
    table: the table to view.
    rows_per_page: how many rows to display at one time.
    fields: an array of field names to display; default is None which uses the full schema.
  Returns:
    A string containing the HTML for the table viewer.
  """
    if not table.exists():
        raise Exception('%s does not exist' % str(table))

    _HTML_TEMPLATE = """
    <div class="bqtv" id="%s"></div>
    <br />%s<br />
    <script>
      require(['extensions/charting', 'element!%s', 'style!/static/extensions/charting.css'],
        function(charts, dom) {
          charts.render(dom,
            {
              chartStyle:"%s",
              dataName:"%s",
              fields:"%s",
              totalRows:%d,
              rowsPerPage:%d,
            }, {}, %s);
        }
      );
    </script>
  """

    if fields is None:
        fields = _utils.get_field_list(fields, table.schema)
    div_id = _html.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)
    if table.job:
        if table.job.cache_hit:
            meta_cost = 'cached'
        else:
            bytes = gcp.bigquery._query_stats.QueryStats._size_formatter(
                table.job.bytes_processed)
            meta_cost = '%s processed' % bytes
        meta_time = 'time: %.1fs' % table.job.total_time
    else:
        meta_cost = ''
        meta_time = ''

    data, total_count = _utils.get_data(table, fields, 0, rows_per_page)

    if total_count < 0:
        # The table doesn't have a length metadata property but may still be small if we fetched less
        # rows than we asked for.
        fetched_count = len(data['rows'])
        if fetched_count < rows_per_page:
            total_count = fetched_count

    chart = 'table' if 0 <= total_count <= rows_per_page else 'paged_table'
    meta_entries = [meta_count, meta_time, meta_cost, meta_name]
    meta_data = '(%s)' % (', '.join(
        [entry for entry in meta_entries if len(entry)]))

    return _HTML_TEMPLATE %\
        (div_id, meta_data, div_id, chart,
         _utils.get_data_source_index(str(table)), ','.join(fields), total_count, rows_per_page,
         json.dumps(data, cls=gcp._util.JSONEncoder))
Beispiel #4
0
def _table_viewer(table, rows_per_page=25, fields=None):
    """  Return a table viewer.

    This includes a static rendering of the first page of the table, that gets replaced
    by the charting code in environments where Javascript is executable and BQ is available.

  Args:
    table: the table to view.
    rows_per_page: how many rows to display at one time.
    fields: an array of field names to display; default is None which uses the full schema.
  Returns:
    A string containing the HTML for the table viewer.
  """
    if not table.exists():
        raise Exception('%s does not exist' % str(table))

    _HTML_TEMPLATE = """
    <div class="bqtv" id="{div_id}">{static_table}</div>
    <br />{meta_data}<br />
    <script>
      require(['extensions/charting', 'element!{div_id}', 'style!/static/extensions/charting.css'],
        function(charts, dom) {{
          charts.render(dom,
            {{
              chartStyle:"{chart_style}",
              dataName:"{data_name}",
              fields:"{fields}",
              totalRows:{total_rows},
              rowsPerPage:{rows_per_page},
            }}, {{}}, {data});
        }}
      );
    </script>
  """

    if fields is None:
        fields = _utils.get_field_list(fields, table.schema)
    div_id = _html.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)
    if table.job:
        if table.job.cache_hit:
            meta_cost = 'cached'
        else:
            bytes = gcp.bigquery._query_stats.QueryStats._size_formatter(
                table.job.bytes_processed)
            meta_cost = '%s processed' % bytes
        meta_time = 'time: %.1fs' % table.job.total_time
    else:
        meta_cost = ''
        meta_time = ''

    data, total_count = _utils.get_data(table, fields, 0, rows_per_page)

    if total_count < 0:
        # The table doesn't have a length metadata property but may still be small if we fetched less
        # rows than we asked for.
        fetched_count = len(data['rows'])
        if fetched_count < rows_per_page:
            total_count = fetched_count

    chart = 'table' if 0 <= total_count <= rows_per_page else 'paged_table'
    meta_entries = [meta_count, meta_time, meta_cost, meta_name]
    meta_data = '(%s)' % (', '.join(
        [entry for entry in meta_entries if len(entry)]))

    return _HTML_TEMPLATE.format(
        div_id=div_id,
        static_table=_html.HtmlBuilder.render_chart_data(data),
        meta_data=meta_data,
        chart_style=chart,
        data_name=_utils.get_data_source_index(str(table)),
        fields=','.join(fields),
        total_rows=total_count,
        rows_per_page=rows_per_page,
        data=json.dumps(data, ensure_ascii=False, cls=gcp._util.JSONEncoder))