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

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

""" 

@file 

@brief Snap rendering in a notebook. 

""" 

import uuid 

import os 

import glob 

from .snap import __file__ as location_js_snap 

 

 

class RenderSnapRaw(object): 

""" 

Renders `Snap <https://snap.berkeley.edu/>`_ using javascript. 

""" 

 

def __init__(self, width="1000", height="600", divid=None, filename=None): 

""" 

initialize 

 

@param width (str) width 

@param height (str) height 

@param divid (str|None) id of the div 

@param filename (str|None) filename 

""" 

if divid == "scratch_div_id": 

# we should use a static counter but it 

# is very unlikely more than one snap will be added to 

# a notebook 

divid += "_%s" % str(uuid.uuid4()).replace("-", "") 

 

self.filename = filename 

self.divid = divid if divid else str(uuid.uuid4()).replace("-", "") 

self.width = width 

self.height = height 

 

def generate_html(self): 

""" 

Return a couple (HTML, JS). 

""" 

w = self.width 

h = self.height 

divid = self.divid 

 

js_path = os.path.dirname(location_js_snap) 

files = [os.path.split(_)[-1] for _ in glob.glob(js_path + "/*.js")] 

path = "/static/snap/" 

js_libs = [path + _ for _ in files] 

 

html_src = """ 

<div id="__DIV__div" style="position:relative; width:__WIDTH__px; height:__HEIGHT__px;"> 

Snap showing up soon... 

<canvas id="__DIV__" style="width:__WIDTH__px; height:__HEIGHT__px; position:relative; " /> 

</div> 

""".replace("__DIV__", divid).replace("__WIDTH__", w).replace("__HEIGHT__", h) 

test_js = """<script> 

var world__DIV__; 

function loop__DIV__() { 

world__DIV__.doOneCycle(); 

} 

function start_snap__DIV__() { 

var sec = document.getElementsByClassName("__DIV__div"); 

sec.innerHTML = "loading..."; 

world__DIV__ = new WorldMorph(document.getElementById('__DIV__')); 

world__DIV__.worldCanvas.focus(); 

new IDE_Morph().openIn(world__DIV__); 

setInterval(loop__DIV__, 1); 

sec.innerHTML = ""; 

} 

window.setTimeout(start_snap__DIV__,500); 

</script> 

""".replace("__DIV__", divid) 

libs = [ 

'<script type="text/javascript" src="{0}"></script>'.format(l) for l in js_libs] 

libs = "\n".join(libs) 

 

return html_src, libs + "\n" + test_js 

 

 

class RenderSnap(RenderSnapRaw): 

""" 

Render Snap using javascript, outputs only HTML. 

""" 

 

def _repr_html_(self): 

ht, js = self.generate_html() 

ht += "{0}".format(js) 

return ht