How to use the notebook.connectors.base.Notebook function in notebook

To help you get started, we’ve selected a few notebook examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cloudera / hue / desktop / libs / notebook / src / notebook / tests.py View on Github external
],
          "type": "query-hive",
          "id": null,
          "snippets": [{"id": "e069ef32-5c95-4507-b961-e79c090b5abf","type":"hive","status":"ready","database":"default","statement":"select * from web_logs","statement_raw":"select * from web_logs","variables":[],"properties":{"settings":[],"files":[],"functions":[]},"result":{}}],
          "uuid": "8a20da5f-b69c-4843-b17d-dea5c74c41d1"
      }
      """

    # Assert that the notebook is first saved
    response = self.client.post(reverse('notebook:save_notebook'), {'notebook': trash_notebook_json})
    data = json.loads(response.content)
    assert_equal(0, data['status'], data)

    # Test that deleting it moves it to the user's Trash folder
    notebook_doc = Document2.objects.get(id=data['id'])
    trash_notebooks = [Notebook(notebook_doc).get_data()]
    response = self.client.post(reverse('notebook:delete'), {'notebooks': json.dumps(trash_notebooks)})
    data = json.loads(response.content)
    assert_equal(0, data['status'], data)
    assert_equal('Trashed 1 notebook(s)', data['message'], data)

    response = self.client.get('/desktop/api2/doc', {'path': '/.Trash'})
    data = json.loads(response.content)
    trash_uuids = [doc['uuid'] for doc in data['children']]
    assert_true(notebook_doc.uuid in trash_uuids, data)

    # Test that any errors are reported in the response
    nonexistant_doc = {
      "id": 12345,
      "uuid": "ea22da5f-b69c-4843-b17d-dea5c74c41d1",
      "selectedSnippet": "hive",
      "showHistory": False,
github cloudera / hue / desktop / libs / notebook / src / notebook / api.py View on Github external
def _get_snippet(user, notebook, snippet, operation_id):
  if operation_id or not snippet:
    nb_doc = Document2.objects.get_by_uuid(user=user, uuid=operation_id or notebook['uuid'])
    notebook = Notebook(document=nb_doc).get_data()
    snippet = notebook['snippets'][0]
  return snippet
github cloudera / hue / desktop / libs / notebook / src / notebook / api.py View on Github external
def _get_statement(notebook):
  if notebook['snippets'] and len(notebook['snippets']) > 0:
    return Notebook.statement_with_variables(notebook['snippets'][0])
  return ''
github cloudera / hue / desktop / libs / notebook / src / notebook / dashboard_api.py View on Github external
def _get_query(self, name):
    nb_doc = Document2.objects.document(user=self.user, doc_id=name)
    notebook = Notebook(document=nb_doc).get_data()
    snippet = notebook['snippets'][0]
    return snippet['statement'].strip(';')
github cloudera / hue / desktop / core / src / desktop / api2.py View on Github external
'dependents': [],
    'data': '',
    'status': 0
  }

  response['user_perms'] = {
    'can_read': document.can_read(request.user),
    'can_write': document.can_write(request.user)
  }

  if with_data:
    data = json.loads(document.data)
    # Upgrade session properties for Hive and Impala
    if document.type.startswith('query'):
      from notebook.models import upgrade_session_properties
      notebook = Notebook(document=document)
      notebook = upgrade_session_properties(request, notebook)
      data = json.loads(notebook.data)
      if document.type == 'query-pig': # Import correctly from before Hue 4.0
        properties = data['snippets'][0]['properties']
        if 'hadoopProperties' not in properties:
          properties['hadoopProperties'] = []
        if 'parameters' not in properties:
          properties['parameters'] = []
        if 'resources' not in properties:
          properties['resources'] = []
      if data.get('uuid') != document.uuid: # Old format < 3.11
        data['uuid'] = document.uuid

    response['data'] = data

  if with_dependencies:
github cloudera / hue / apps / oozie / src / oozie / views / editor2.py View on Github external
script_path = node_data.get('properties', {}).get('script_path', {})
    if script_path:
      script_path = script_path.replace('hdfs://', '')

      if request.fs.do_as_user(request.user, request.fs.exists, script_path):
        data = request.fs.do_as_user(request.user, request.fs.read, script_path, 0, 16 * 1024 ** 2)

        if node_data['type'] in ('hive', 'hive2'):
          parameters = parameters.union(set(find_dollar_braced_variables(data)))
        elif node_data['type'] == 'pig':
          parameters = parameters.union(set(find_dollar_variables(data)))
    elif node_data['type'] == 'hive-document':
      notebook = Notebook(document=Document2.objects.get_by_uuid(user=request.user, uuid=node_data['properties']['uuid']))
      parameters = parameters.union(set(find_dollar_braced_variables(notebook.get_str())))
    elif node_data['type'] == 'sqoop-document':
      notebook = Notebook(document=Document2.objects.get_by_uuid(user=request.user, uuid=node_data['properties']['uuid']))
      parameters = parameters.union(set(find_dollar_braced_variables(notebook.get_str())))
    elif node_data['type'] == 'spark-document':
      notebook = Notebook(document=Document2.objects.get_by_uuid(user=request.user, uuid=node_data['properties']['uuid']))
      for arg in notebook.get_data()['snippets'][0]['properties']['spark_arguments']:
        parameters = parameters.union(set(find_dollar_braced_variables(arg)))

    response['status'] = 0
    response['parameters'] = list(parameters)
  except Exception as e:
    response['message'] = str(e)

  return JsonResponse(response)
github cloudera / hue / desktop / libs / dashboard / src / dashboard / models.py View on Github external
def get_default(self, user, name, engine='solr', source='data'):
    fields = self.fields_data(user, name, engine, source=source)
    id_field = [field['name'] for field in fields if field.get('isId')]

    if id_field:
      id_field = id_field[0]
    else:
      id_field = '' # Schemaless might not have an id

    if source == 'query':
      nb_doc = Document2.objects.document(user=user, doc_id=name)
      notebook = Notebook(document=nb_doc).get_data()
      label = _get_snippet_name(notebook, unique=True)
    else:
      label = name

    TEMPLATE = {
      "extracode": escape("<style type="\&quot;text/css\&quot;">\nem {\n  font-weight: bold;\n  background-color: yellow;\n}</style>\n\n"),
      "highlighting": [""],
      "properties": {"highlighting_enabled": True},
      "template": """
      <div class="row-fluid">
        <div class="row-fluid">
          <div class="span12">%s</div>
        </div>
        <br>
      </div>""" % ' '.join(['{{%s}}' % field['name'] for field in fields]),
      "isGridLayout": True,
github cloudera / hue / desktop / libs / notebook / src / notebook / api.py View on Github external
@require_POST
@check_document_access_permission()
@api_error_handler
def get_external_statement(request):
  response = {'status': -1, 'message': ''}

  notebook = json.loads(request.POST.get('notebook', '{}'))
  snippet = json.loads(request.POST.get('snippet', '{}'))

  if snippet.get('statementType') == 'file':
    response['statement'] = _get_statement_from_file(request.user, request.fs, snippet)
  elif snippet.get('statementType') == 'document':
    notebook = Notebook(Document2.objects.get_by_uuid(user=request.user, uuid=snippet['associatedDocumentUuid'], perm_type='read'))
    response['statement'] = notebook.get_str()

  response['status'] = 0

  return JsonResponse(response)