-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy path_ipython.py
More file actions
88 lines (70 loc) · 2.78 KB
/
_ipython.py
File metadata and controls
88 lines (70 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
_ipython.py
This module provides IPython magic functions for integrating Q# code
execution within Jupyter notebooks.
"""
from time import monotonic
from IPython.display import display, Javascript, clear_output
from IPython.core.magic import register_cell_magic
from ._native import QSharpError
from ._qsharp import _get_default_ctx, qsharp_value_to_python_value
from . import telemetry_events
import pathlib
def register_magic():
@register_cell_magic
def qsharp(line, cell):
"""Cell magic to interpret Q# code in Jupyter notebooks."""
# This effectively pings the kernel to ensure it recognizes the cell is running and helps with
# accureate cell execution timing.
clear_output()
def callback(output):
display(output)
# This is a workaround to ensure that the output is flushed. This avoids an issue
# where the output is not displayed until the next output is generated or the cell
# is finished executing.
display(display_id=True)
telemetry_events.on_run_cell()
start_time = monotonic()
try:
results = qsharp_value_to_python_value(
_get_default_ctx()._interpreter.interpret(cell, callback)
)
durationMs = (monotonic() - start_time) * 1000
telemetry_events.on_run_cell_end(durationMs)
return results
except QSharpError as e:
# pylint: disable=raise-missing-from
raise QSharpCellError(str(e))
def enable_classic_notebook_codemirror_mode():
"""
Registers %%qsharp cells with MIME type text/x-qsharp
and defines a CodeMirror mode to enable syntax highlighting.
This only works in "classic" Jupyter notebooks, not Notebook v7.
"""
js_to_inject = open(
pathlib.Path(__file__)
.parent.resolve()
.joinpath(".data", "qsharp_codemirror.js"),
mode="r",
encoding="utf-8",
).read()
# Extend the JavaScript display helper to print nothing when used
# in a non-browser context (i.e. IPython console)
class JavaScriptWithPlainTextFallback(Javascript):
def __repr__(self):
return ""
# This will run the JavaScript in the context of the frontend.
display(JavaScriptWithPlainTextFallback(js_to_inject))
class QSharpCellError(BaseException):
"""
Error raised when a %%qsharp cell fails.
"""
def __init__(self, traceback: str):
self.traceback = traceback.splitlines()
def _render_traceback_(self):
# We want to specifically override the traceback so that
# the Q# error directly from the interpreter is shown
# instead of the Python error.
return self.traceback