Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"). You 

4# may not use this file except in compliance with the License. A copy of 

5# the License is located at 

6# 

7# http://aws.amazon.com/apache2.0/ 

8# 

9# or in the "license" file accompanying this file. This file is 

10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 

11# ANY KIND, either express or implied. See the License for the specific 

12# language governing permissions and limitations under the License. 

13 

14import sys 

15 

16 

17def running_in_jupyter(): 

18 """ 

19 Determine if running within Jupyter. 

20 

21 Inspired by https://github.com/ipython/ipython/issues/11694 

22 

23 Returns: 

24 bool: True if running in Jupyter, else False. 

25 """ 

26 in_ipython = False 

27 in_ipython_kernel = False 

28 

29 # if IPython hasn't been imported, there's nothing to check 

30 if "IPython" in sys.modules: 

31 get_ipython = sys.modules["IPython"].__dict__["get_ipython"] 

32 

33 ip = get_ipython() 

34 in_ipython = ip is not None 

35 

36 if in_ipython: 

37 in_ipython_kernel = getattr(ip, "kernel", None) is not None 

38 

39 return in_ipython_kernel