def build_context(): """Builds a javascript context.""" threejs_url = 'https://www.gstatic.com/external_hosted/threejs-r98/' _publish.javascript(url=threejs_url + 'three.min.js') _publish.javascript(url=threejs_url + 'examples/js/controls/OrbitControls.js') return _js_builder.Js(mode=_js_builder.PERSISTENT)
def _triangular_mesh_to_three_geometry(vertices, faces, vertex_colors=None): """Converts a triangular mesh to a Three.js BufferGeometry object. Args: vertices: a [V,3] numpy ndarray containing the position of the vertices composing the mesh. V denotes the number of vertices. Vertex positions are expected to be floating values. faces: a [F,3] numpy array describing the vertices contained in each face of the mesh. F denotes the number of faces. The values of that array are expected to be positive integer values. vertex_colors: a [V, 3] numpy array describing the RGB color of each vertex in the mesh. V denotes the number of vertices. Each channel in vertex colors is expected to be floating values in range [0, 1]. Returns: A BufferGeometry object describing the geometry of the mesh and which can be consumed by Three.js. """ context = _js_builder.Js(mode=_js_builder.PERSISTENT) vertices = context.Float32Array.new_object(vertices.ravel().tolist()) faces = context.Uint32Array.new_object(faces.ravel().tolist()) geometry = context.THREE.BufferGeometry.new_object() geometry.addAttribute( 'position', context.THREE.BufferAttribute.new_object(vertices, 3)) geometry.setIndex(context.THREE.BufferAttribute.new_object(faces, 1)) geometry.computeVertexNormals() if vertex_colors is not None: vertex_colors = context.Float32Array.new_object( vertex_colors.ravel().tolist()) geometry.addAttribute( 'color', context.THREE.BufferAttribute.new_object(vertex_colors, 3)) return geometry
def colab_play(array_of_floats, sample_rate, ephemeral=True, autoplay=False): """Creates an HTML5 audio widget to play a sound in Colab. This function should only be called from a Colab notebook. Args: array_of_floats: A 1D or 2D array-like container of float sound samples. Values outside of the range [-1, 1] will be clipped. sample_rate: Sample rate in samples per second. ephemeral: If set to True, the widget will be ephemeral, and disappear on reload (and it won't be counted against realtime document size). autoplay: If True, automatically start playing the sound when the widget is rendered. """ from google.colab.output import \ _js_builder as js # pylint:disable=g-import-not-at-top,protected-accessk,import-error normalizer = float(np.iinfo(np.int16).max) array_of_ints = np.array(np.asarray(array_of_floats) * normalizer, dtype=np.int16) memfile = io.BytesIO() wavfile.write(memfile, sample_rate, array_of_ints) html = """<audio controls {autoplay}> <source controls src="data:audio/wav;base64,{base64_wavfile}" type="audio/wav" /> Your browser does not support the audio element. </audio>""" html = html.format(autoplay='autoplay' if autoplay else '', base64_wavfile=base64.b64encode( memfile.getvalue()).decode('ascii')) memfile.close() global _play_id _play_id += 1 if ephemeral: element = 'id_%s' % _play_id display.display(display.HTML('<div id="%s"> </div>' % element)) js.Js('document', mode=js.EVAL).getElementById(element).innerHTML = html else: display.display(display.HTML(html))
def _set_output_area(selector): if isinstance(selector, six.string_types): element = _js_builder.Js('document').querySelector(selector) else: element = selector _jsapi.output.setActiveOutputArea(element)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language govestylerning permissions and # limitations under the License. """Support for custom output areas in colab.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import contextlib import six from google.colab.output import _js_builder _jsapi = _js_builder.Js('google.colab') def _set_output_area(selector): if isinstance(selector, six.string_types): element = _js_builder.Js('document').querySelector(selector) else: element = selector _jsapi.output.setActiveOutputArea(element) @contextlib.contextmanager def redirect_to_element(selector): """Will redirect all output to a given element. Args: