microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/qsharp/openqasm/_ipython.py
24lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from .._native import Output # type: ignore |
| 5 | |
| 6 | _in_jupyter = False |
| 7 | try: |
| 8 | from IPython.display import display |
| 9 | |
| 10 | if get_ipython().__class__.__name__ == "ZMQInteractiveShell": # type: ignore |
| 11 | _in_jupyter = True # Jupyter notebook or qtconsole |
| 12 | except: |
| 13 | pass |
| 14 | |
| 15 | |
| 16 | def display_or_print(output: Output) -> None: |
| 17 | if _in_jupyter: |
| 18 | try: |
| 19 | display(output) |
| 20 | return |
| 21 | except: |
| 22 | # If IPython is not available, fall back to printing the output |
| 23 | pass |
| 24 | print(output, flush=True) |
| 25 | |