Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Synapse]Reimport notebook Failure #18718

Merged
merged 13 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/azure-cli/azure/cli/command_modules/synapse/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@
- name: Create a notebook.
text: |-
az synapse notebook create --workspace-name testsynapseworkspace \\
--name testnotebook --file @path/notebook.json
--name testnotebook --file @path/notebook.ipynb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean .json is preferred for notbook file? Can .ipynb file still work well?
I remember @idear1203 insisting on .ipynb. (See #17661 (comment))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evelyn-ys , thanks for confirmation. Yes, we still want to use .ipynb in this command parameter. The reason of change here is, we want to correct the documentation to make it clear to our customers that .ipynb is the desired file format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinging @evelyn-ys , thanks a lot!

"""

helps['synapse notebook set'] = """
Expand All @@ -1448,7 +1448,7 @@
- name: Set an exist notebook.
text: |-
az synapse notebook set --workspace-name testsynapseworkspace \\
--name testnotebook --file @path/notebook.json
--name testnotebook --file @path/notebook.ipynb
"""

helps['synapse notebook import'] = """
Expand All @@ -1458,7 +1458,7 @@
- name: Import a notebook.
text: |-
az synapse notebook import --workspace-name testsynapseworkspace \\
--name testnotebook --file @path/notebook.json
--name testnotebook --file @path/notebook.ipynb
"""

helps['synapse notebook show'] = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# --------------------------------------------------------------------------------------------

import json
import logging
import os
from azure.synapse.artifacts.models import (LinkedService, Dataset, PipelineResource, RunFilterParameters,
Trigger, DataFlow, BigDataPoolReference, NotebookSessionProperties,
Expand Down Expand Up @@ -255,13 +256,6 @@ def get_notebook(cmd, workspace_name, notebook_name):


def export_notebook(cmd, workspace_name, output_folder, notebook_name=None):
def write_to_file(notebook, path):
try:
with open(path, 'w') as f:
json.dump(notebook.properties.as_dict(), f, indent=4)
except IOError:
raise CLIError('Unable to export to file: {}'.format(path))

client = cf_synapse_notebook(cmd.cli_ctx, workspace_name)
if notebook_name is not None:
notebook = client.get_notebook(notebook_name)
Expand All @@ -276,6 +270,77 @@ def write_to_file(notebook, path):
write_to_file(notebook, path)


def metadata_processing(notebook_properties, displayedWidgets):
synapseWidgetNotebookMetadataVersion = '0.1'
metadata = {}
notebook_properties_metadata = {}
for key in list(notebook_properties.keys()):
if key == 'metadata':
notebook_properties_metadata = notebook_properties['metadata']

if notebook_properties_metadata is None:
return metadata, displayedWidgets

for elementkey in list(notebook_properties_metadata.keys()):
if elementkey == 'language_info':
if notebook_properties_metadata['language_info'] and \
'codemirror_mode' in notebook_properties_metadata['language_info']:
notebook_properties_metadata['language_info'].pop('codemirror_mode')
metadata['language_info'] = notebook_properties_metadata['language_info']
elif elementkey == 'description':
metadata['description'] = notebook_properties_metadata['description']
elif elementkey == 'saveOutput':
metadata['save_output'] = notebook_properties_metadata['saveOutput']
elif elementkey == 'kernelspec':
metadata['kernelspec'] = notebook_properties_metadata['kernelspec']
elif elementkey == 'synapse_widget' and \
'state' in notebook_properties_metadata['synapse_widget']:
for ekey in list(notebook_properties_metadata['synapse_widget']['state'].keys()):
for i in reversed(range(len(displayedWidgets))):
if displayedWidgets[i]['widget_id'] == ekey:
displayedWidgets.pop(i)
metadata['synapse_widget'] = notebook_properties_metadata['synapse_widget']
metadata['synapse_widget']['version'] = synapseWidgetNotebookMetadataVersion
return metadata, displayedWidgets


def write_to_file(notebook, path):
try:
notebook_properties = notebook.properties.as_dict()
livyStatementMetaOutputContentType = 'application/vnd.livy.statement-meta+json'
synapseWidgetViewOutputContentType = 'application/vnd.synapse.widget-view+json'
notebook_result = {}
displayedWidgets = []
notebook_result['nbformat'] = 4
notebook_result['nbformat_minor'] = 2
for cell in notebook_properties['cells']:
if cell['cell_type'] == 'code' and cell['outputs']:
for output in cell['outputs']:
if output['output_type'] == 'display_data' and \
synapseWidgetViewOutputContentType in output['data']:
displayedWidgets.append(output["data"]["application/vnd.synapse.widget-view+json"])

metadata_results, displayedWidgets_results = \
metadata_processing(notebook_properties, displayedWidgets)

if len(displayedWidgets_results) > 0:
logging.info('Detected widget with missing data.')

for cell in notebook_properties['cells']:
if cell['cell_type'] == 'code' and cell['outputs']:
for output in cell['outputs']:
if output['output_type'] == 'display_data' and output['data'] and \
livyStatementMetaOutputContentType in output['data']:
output['data'].pop(livyStatementMetaOutputContentType)

notebook_result['metadata'] = metadata_results
notebook_result['cells'] = notebook_properties['cells']
with open(path, 'w') as f:
json.dump(notebook_result, f, indent=4)
except IOError:
raise CLIError('Unable to export to file: {}'.format(path))


def delete_notebook(cmd, workspace_name, notebook_name, no_wait=False):
client = cf_synapse_notebook(cmd.cli_ctx, workspace_name)
return sdk_no_wait(no_wait, client.begin_delete_notebook, notebook_name, polling=True)