Coverage for src/code_beatrix/ipythonhelper/nbcanvas.py: 100%
15 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-29 13:45 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-04-29 13:45 +0200
1"""
2@file
3@brief Drawing from a canvas
4"""
5import IPython.core.display as ipydisplay
6from IPython.display import display_html, display_javascript, Javascript, HTML
7from .path_helper import local_d3js
10def display_canvas_point(html_id, width=800, heigth=400, var_name="points"):
11 """
12 Adds a canvas to draw from a notebook, the code use javascript.
14 @param height height
15 @param width width
16 @param html_id the function adds a div section, it should be different for every canevas
17 @param var_name the function adds this variable to the kernel workspace
19 @return the list of points
20 """
21 js_libs = local_d3js()
23 # http://jsfiddle.net/yonester/9tr7w/2/
24 html_src = """
25 <div id="{0}"></div>
26 """.format(html_id)
28 points = []
30 test_d3_js = """
31 var r;
33 var command0 ="__VARNAME__=[]";
34 var kernel = IPython.notebook.kernel;
35 kernel.execute(command0);
37 var vis = d3.select("#__ID__").append("svg")
38 .attr("width", __WIDTH__)
39 .attr("height", __HEIGHT__)
40 .style('border', '1px solid black')
41 .on("mousedown", mousedown)
42 ;
44 function mousedown() {
45 var m = d3.mouse(this);
46 r = vis.append("rect")
47 .attr("class", "myrect")
48 .attr("style", "fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,255);")
49 .attr("x", m[0])
50 .attr("y", m[1])
51 .attr("width", 5)
52 .attr("height", 5);
53 var command = "__VARNAME__.append( (" + m[0] + ", -" + m[1] + ") )";
54 kernel.execute(command);
55 }
56 """ .replace("__WIDTH__", str(width)) \
57 .replace("__HEIGHT__", str(heigth)) \
58 .replace("__ID__", html_id)\
59 .replace("__VARNAME__", var_name)
60 js_libs = [js_libs]
61 if 'display' not in dir(ipydisplay):
62 # Weird bug introduced in IPython 8.0.0
63 import IPython.core.display_functions
64 ipydisplay.display = IPython.core.display_functions.display
65 display_html(HTML(data=html_src))
66 display_javascript(Javascript(data=test_d3_js, lib=js_libs))
68 return points