def Submit(exercise_id=None): if JWT_TOKEN == "": display.display(display.HTML("Please get JWT_TOKEN by visiting " + "<a href='" + SERVER_URL + "/login'>Login page</a>")) raise Exception("Please set JWT_TOKEN") notebook = google_message.blocking_request( "get_ipynb", request="", timeout_sec=120)["ipynb"] ids = [] for cell in notebook['cells']: if 'metadata' not in cell: continue m = cell['metadata'] if m and 'exercise_id' in m: cell_id = m['exercise_id'] if cell_id: ids.append(cell_id) params = {} if exercise_id: if exercise_id not in ids: raise Exception('Not valid exercise ID: ' + exercise_id + ". Valid ids: " + ", ".join(ids)) params["exercise_id"] = exercise_id data = json.dumps(notebook) r = requests.post(SERVER_URL + "/upload", files={"notebook": data}, headers={"Authorization": "Bearer " + JWT_TOKEN}, params=params) if r.status_code == 401: display.display(display.HTML("Not authorized: is your JWT_TOKEN correct? " + "Please get JWT_TOKEN by visiting " + "<a target='_blank' href='" + SERVER_URL + "/login'>Login page</a>" + "in a new browser tab.")) display.display(display.HTML(r.content.decode('utf-8')))
def test_encourage_iframe_over_html(m_warn): display.HTML() m_warn.assert_not_called() display.HTML('<br />') m_warn.assert_not_called() display.HTML('<html><p>Lots of content here</p><iframe src="http://a.com"></iframe>') m_warn.assert_not_called() display.HTML('<iframe src="http://a.com"></iframe>') m_warn.assert_called_with('Consider using IPython.display.IFrame instead') m_warn.reset_mock() display.HTML('<IFRAME SRC="http://a.com"></IFRAME>') m_warn.assert_called_with('Consider using IPython.display.IFrame instead')
def to_tfr(self, output_dir: str, schema_map: Dict[str, schema.SchemaMap] = schema.image_csv_schema, runner: str = 'DirectRunner', project: Optional[str] = None, region: Optional[str] = None, tfrecorder_wheel: Optional[str] = None, dataflow_options: Union[Dict[str, Any], None] = None, job_label: str = 'to-tfr', compression: Optional[str] = 'gzip', num_shards: int = 0) -> Dict[str, Any]: """TFRecorder Pandas Accessor. TFRecorder provides an easy interface to create image-based tensorflow records from a dataframe containing GCS locations of the images and labels. Usage: import tfrecorder df.tfrecorder.to_tfr( output_dir='gcs://foo/bar/train', runner='DirectRunner', compression='gzip', num_shards=10) Args: schema_map: A dict mapping column names to supported types. output_dir: Local directory or GCS Location to save TFRecords to. Note: GCS required for DataflowRunner runner: Beam runner. Can be DirectRunner or DataflowRunner. project: GCP project name (Required if DataflowRunner). region: GCP region name (Required if DataflowRunner). tfrecorder_wheel: Path to the tfrecorder wheel Dataflow will run. (create with 'python setup.py sdist' or 'pip download tfrecorder --no-deps') dataflow_options: Optional dictionary containing Dataflow options. job_label: User supplied description for the beam job name. compression: Can be 'gzip' or None for no compression. num_shards: Number of shards to divide the TFRecords into. Default is 0 = no sharding. Returns: job_results: A dictionary of job results. """ display.display( display.HTML('<b>Logging output to /tmp/{} </b>'.format( constants.LOGFILE))) r = client.create_tfrecords(self._df, output_dir=output_dir, schema_map=schema_map, runner=runner, project=project, region=region, tfrecorder_wheel=tfrecorder_wheel, dataflow_options=dataflow_options, job_label=job_label, compression=compression, num_shards=num_shards) return r
def display_graph(self): """Displays graph via IPython or prints DOT if not possible.""" try: from IPython.core import display # pylint: disable=import-error display.display(display.HTML(self._get_graph().create_svg())) # pylint: disable=protected-access except ImportError: print(str(self._get_graph()))
def signature(self, line): sig = '''Author: <a href="https://ramiro.org/">Ramiro Gómez</a> • Last edited: {}<br>{} {} - {} {} - IPython {} - matplotlib {} - numpy {} - pandas {}'''.format( datetime.now().strftime('%B %d, %Y'), platform.system(), platform.release(), platform.python_implementation(), platform.python_version(), IPython.__version__, matplotlib.__version__, numpy.__version__, pandas.__version__) return display.HTML(sig)
def report(template, **kwargs): """Non-magic version of the report. This function can be used as report(template, source=submission_source.source, results=results) The keyword arguments are forwarded to the invocation of `template.render()`. If `source` keyword argument is present, a syntax-highlighted HTML copy of it is additionally passed with `formatted_source` keyword argument. Args: template - A template earlier defined with %%template magic, or an inline test. In case of an inline test, the template is automatically defined to include the source code (if provided) and the error message from the inline test result. Returns: A displayable HTML object. """ if 'source' in kwargs: kwargs['formatted_source'] = pygments.highlight( kwargs['source'], lexers.PythonLexer(), # pylint: disable=E1101 formatters.HtmlFormatter()).rstrip() # pylint: disable=E1101 # Render the template giving the specified variable as 'results', # and render the result as inlined HTML in cell output. 'source' is # the prerendered source code. if isinstance(template, jinja2.Template): html = template.render(**kwargs) elif isinstance(template, types.SimpleNamespace) and template.type == 'inlinetest': source_template = """ <h4 style='color: #387;'>Your submission</h4> <pre style='background: #F0F0F0; padding: 3pt; margin: 4pt; border: 1pt solid #DDD; border-radius: 3pt;'>{{ formatted_source }}</pre>""" result_template = """ <h4 style='color: #387;'>Results</h4> {% if 'passed' in results and results['passed'] %} Looks OK. {% elif 'error' in results %} {{results['error'] | e}} {% else %} Something is wrong. {% endif %}""" if 'formatted_source' in kwargs: template_source = source_template else: template_source = '' template_source += result_template actual_template = jinja2.Template(template_source) html = actual_template.render(**kwargs) else: raise Exception("Unrecognized template argument of class %s" % (test_case.__class__)) return display.HTML(html)
def signature(self, line): sig = '''Author: <a href="https://twitter.com/thuramg">Thura Z. Neumann</a> • Last edited: {}<br>{} {} - {} {} - IPython {} - matplotlib {} - numpy {} - pandas {} - scikit-learn {}'''.format( datetime.now().strftime('%B %d, %Y'), platform.system(), platform.release(), platform.python_implementation(), platform.python_version(), IPython.__version__, matplotlib.__version__, numpy.__version__, pandas.__version__, sklearn.__version__) return display.HTML(sig)
def display_graph(self): rendered_graph = self._renderer.render_pipeline_graph(self) if ie.current_env().is_in_notebook: try: from IPython.core import display display.display(display.HTML(rendered_graph)) except ImportError: # Unlikely to happen when is_in_notebook. logging.warning('Failed to import IPython display module when current ' 'environment is in a notebook. Cannot display the ' 'pipeline graph.')
def get_usgs_RDB_service(url, headers=None, params=None): """Request data from a USGS dataservice and handle errors. Args: url (str): a string used by Requests as the base URL. header (dict): a dict of parameters used to request the data. params (dict): a dict of parameters used to modify the url of a REST service. Returns: A Requests response object. Raises: This function will raise an exception for any non-200 status code, and\ in cases where the USGS service returns anything that is not obviously\ an RDB file. If an exception is raised, then an attempt will be made to\ display the error page which the USGS sometimes sends back to the user. """ response = requests.get(url, headers=headers, params=params) if response.status_code == 200: if response.text[0] == "#": # Everything seems good; they apparently returned an RDB file. return response else: print("The USGS has apparently not returned any data. Check the " "following message for further information for why this " "request failed. One possibility is that your site number " "is incorrect.") display.display(display.HTML(response.text)) raise exceptions.HydroNoDataError( "The USGS did not return a valid RDB file " "for this request.") else: # response.status_code != 200: print(f"The USGS has returned an error code of {response.status_code}") # If this code is being run inside of a notebook, the USGS error page # will be displayed. display.display(display.HTML(response.text)) # raise an exception response.raise_for_status() # or raise some sort of Hydro http error based on requests http error. return response
def render_graph(graph_type, dict_data, bind_css=None, css_file_names=None): # Pre-process data utils.preprocess_data(graph_type, dict_data) if bind_css is None and utils.get_default_css_binding( graph_type) is not None: bind_css = utils.get_default_css_binding(graph_type) result = display.HTML( _render_graph(graph_type, dict_data, bind_css, css_file_names)) return result
def load_ipython_extension(ip): """Load the extension in IPython.""" ip.register_magics(PigMagics) # enable Pig highlight js = display.Javascript(data=js_string) display.display_javascript(js) # some custom CSS to augment the syntax highlighting css = display.HTML(css_string) display.display_html(css)
def signature(self, line): sig = '''Author: <a href="https://github.com/coding-to-music">@ThomasConnors</a> • Last edited: {}<br>{} {} - {} {} - IPython {} - matplotlib {} - numpy {} - pandas {}'''.format( datetime.now().strftime('%B %d, %Y'), platform.system(), platform.release(), platform.python_implementation(), platform.python_version(), IPython.__version__, matplotlib.__version__, numpy.__version__, pandas.__version__) return display.HTML(sig)
def test_displayobject_repr(): h = display.HTML('<br />') nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') h._show_mem_addr = True nt.assert_equal(repr(h), object.__repr__(h)) h._show_mem_addr = False nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') j = display.Javascript('') nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') j._show_mem_addr = True nt.assert_equal(repr(j), object.__repr__(j)) j._show_mem_addr = False nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
def to_tfr(self, output_dir: str, runner: str = 'DirectRunner', project: Optional[str] = None, region: Optional[str] = None, dataflow_options: Union[Dict[str, Any], None] = None, job_label: str = 'to-tfr', compression: Optional[str] = 'gzip', num_shards: int = 0) -> Dict[str, Any]: """TFRecorder Pandas Accessor. TFRecorder provides an easy interface to create image-based tensorflow records from a dataframe containing GCS locations of the images and labels. Usage: import tfrecorder df.tfrecorder.to_tfr( output_dir='gcs://foo/bar/train', runner='DirectRunner', compression='gzip', num_shards=10) Args: output_dir: Local directory or GCS Location to save TFRecords to. runner: Beam runner. Can be DirectRunner or DataFlowRunner. project: GCP project name (Required if DataFlowRunner). region: GCP region name (Required if DataFlowRunner). dataflow_options: Optional dictionary containing DataFlow options. job_label: User supplied description for the beam job name. compression: Can be 'gzip' or None for no compression. num_shards: Number of shards to divide the TFRecords into. Default is 0 = no sharding. Returns: job_results: A dictionary of job results. """ display.display( display.HTML('<b>Logging output to /tmp/{} </b>'.format( constants.LOGFILE))) r = client.create_tfrecords(self._df, output_dir=output_dir, runner=runner, project=project, region=region, dataflow_options=dataflow_options, job_label=job_label, compression=compression, num_shards=num_shards) return r
def update_display(self, force=False): """Updates display on the frontend. Retrieves the latest execution status by querying CacheManager and updates display on the fronend. The assumption is that there is only one pipeline in a cell, because it clears up everything in the cell output every update cycle. Args: force: (bool) whether to force updating when no stats change happens. """ with self._lock: stats_updated = False for pcoll_id, stats in self._pcollection_stats.items(): cache_label = stats['cache_label'] version = stats['version'] if force or not self._cache_manager.is_latest_version( version, 'sample', cache_label): pcoll_list, version = self._cache_manager.read( 'sample', cache_label) stats['sample'] = pcoll_list stats['version'] = version stats_updated = True if pcoll_id in self._analyzer.tl_referenced_pcoll_ids(): self._text_to_print[pcoll_id] = (str( '%s produced %s' % (self._producers[pcoll_id], interactive_pipeline_graph.format_sample( pcoll_list, 5)))) if force or stats_updated: self._pipeline_graph.update_pcollection_stats( self._pcollection_stats) if IPython: from IPython.core import display display.clear_output(True) rendered_graph = unicode( self._renderer.render_pipeline_graph( self._pipeline_graph)) display.display(display.HTML(rendered_graph)) _display_progress('Running...') for text in self._text_to_print.values(): if text: _display_progress(text)
def show_google_maps_link(longitude, latitude): """Creates Google Maps link for a given coordinate. Args: longitude: float. latitude: float. Returns: IPython.core.display.display element. """ GOOGLE_MAPS_LINK_TEMPLATE = ( '<a target="_blank" href="https://www.google.com/maps/place/%f,%f">Google Maps</a>' ) html_text = GOOGLE_MAPS_LINK_TEMPLATE % (latitude, longitude) return display.display(display.HTML(html_text))
def report(template, **kwargs): """Non-magic version of the report. This function can be used as report(template, source=submission_source.source, results=results) The keyword arguments are forwarded to the invocation of `template.render()`. If `source` keyword argument is present, a syntax-highlighted HTML copy of it is additionally passed with `formatted_source` keyword argument. """ if 'source' in kwargs: kwargs['formatted_source'] = pygments.highlight( kwargs['source'], lexers.PythonLexer(), formatters.HtmlFormatter()) # pylint: disable=E1101 # Render the template giving the specified variable as 'results', # and render the result as inlined HTML in cell output. 'source' is # the prerendered source code. return display.HTML(template.render(**kwargs))
def report(self, line, cell): """Renders the named template. Syntax: %%report results_var template_name """ var_name = line template_name = cell template = self.shell.ev(template_name) results = self.shell.ev(var_name) source = self.shell.user_ns['submission_source'].source formatted_source = pygments.highlight( source, lexers.PythonLexer(), formatters.HtmlFormatter()).rstrip() # Render the template giving the specified variable as 'results', # and render the result as inlined HTML in cell output. 'source' is # the prerendered source code. return display.HTML( template.render(results=results, source=source, formatted_source=formatted_source))
def display_template(template_name): file_name = f'pokemon/templates/{template_name}' with open(file_name, 'r') as f: file = escape(f.read()) idisplay.display(idisplay.HTML(f'<pre>{file}</pre>'))
"get_ipynb", request="", timeout_sec=120)["ipynb"] ids = [] for cell in notebook['cells']: if 'metadata' not in cell: continue m = cell['metadata'] if m and 'exercise_id' in m: cell_id = m['exercise_id'] if cell_id: ids.append(cell_id) params = {} if exercise_id: if exercise_id not in ids: raise Exception('Not valid exercise ID: ' + exercise_id + ". Valid ids: " + ", ".join(ids)) params["exercise_id"] = exercise_id data = json.dumps(notebook) r = requests.post(SERVER_URL + "/upload", files={"notebook": data}, headers={"Authorization": "Bearer " + JWT_TOKEN}, params=params) if r.status_code == 401: display.display(display.HTML("Not authorized: is your JWT_TOKEN correct? " + "Please get JWT_TOKEN by visiting " + "<a target='_blank' href='" + SERVER_URL + "/login'>Login page</a>" + "in a new browser tab.")) display.display(display.HTML(r.content.decode('utf-8'))) if JWT_TOKEN == "": display.display(display.HTML("Please get JWT_TOKEN by visiting " + "<a href='" + SERVER_URL + "/login'>Login page</a>")) raise Exception("Please set JWT_TOKEN")
def Check(exercise_id): """Checks one exercise against embedded inline tests.""" def _get_exercise_id(cell): if 'metadata' in cell and 'exercise_id' in cell['metadata']: return cell['metadata']['exercise_id'] if 'source' not in cell or 'cell_type' not in cell or cell[ 'cell_type'] != 'code': return None source = ''.join(cell['source']) m = re.search('(?m)^# *EXERCISE_ID: [\'"]?([a-zA-Z0-9_.-]*)[\'"]? *\n', source) if m: return m.group(1) return None notebook = GetNotebook() # 1. Find the first cell with specified exercise ID. found = False for (i, cell) in enumerate(notebook['cells']): if _get_exercise_id(cell) == exercise_id: found = True break if not found: raise Exception(f'exercise {exercise_id} not found') submission_source = ''.join(cell['source']) # extract the submission cell submission_source = re.sub(r'^%%(solution|submission)[ \t]*\n', '', submission_source) # cut %%solution magic inlinetests = {} if 'metadata' in cell and 'inlinetests' in cell['metadata']: inlinetests = cell['metadata']['inlinetests'] if len(inlinetests) == 0: j = i + 1 # 2. If inline tests were not present in metadata, find the inline tests # that follow this exercise ID. while j < len(notebook['cells']): cell = notebook['cells'][j] if 'source' not in cell or 'cell_type' not in cell or cell[ 'cell_type'] != 'code': j += 1 continue id = _get_exercise_id(cell) source = ''.join(cell['source']) if id == exercise_id: # 3. Pick the last marked cell as submission cell. submission_source = source # extract the submission cell submission_source = re.sub( r'^%%(solution|submission)[ \t]*\n', '', submission_source) # cut %%solution magic j += 1 continue m = re.match(r'^%%inlinetest[ \t]*([a-zA-Z0-9_]*)[ \t]*\n', source) if m: test_name = m.group(1) test_source = source[m.end(0):] # cut %%inlinetest magic # 2a. Store the inline test. inlinetests[test_name] = test_source if id is not None and id != exercise_id: # 4. Stop at the next exercise_id. break j += 1 html = RunInlineTests(submission_source, inlinetests) return display.HTML(html)
def test_html_metadata(): s = "<h1>Test</h1>" h = display.HTML(s, metadata={"isolated": True}) nt.assert_equal(h._repr_html_(), (s, {"isolated": True}))
def show_source(f): import inspect src = inspect.getsource(f) html = hilite_code(src) return display.HTML(html)
def cython(self, line, cell): """Compile and import everything from a Cython code cell. The contents of the cell are written to a `.pyx` file in the directory `IPYTHONDIR/cython` using a filename with the hash of the code. This file is then cythonized and compiled. The resulting module is imported and all of its symbols are injected into the user's namespace. The usage is similar to that of `%%cython_pyximport` but you don't have to pass a module name:: %%cython def f(x): return 2.0*x To compile OpenMP codes, pass the required `--compile-args` and `--link-args`. For example with gcc:: %%cython --compile-args=-fopenmp --link-args=-fopenmp ... """ args = magic_arguments.parse_argstring(self.cython, line) code = cell if cell.endswith('\n') else cell + '\n' lib_dir = os.path.join(get_ipython_cache_dir(), 'cython') quiet = True key = code, sys.version_info, sys.executable, Cython.__version__ if not os.path.exists(lib_dir): os.makedirs(lib_dir) if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += time.time(), module_name = "_cython_magic_" + hashlib.md5( str(key).encode('utf-8')).hexdigest() module_path = os.path.join(lib_dir, module_name + self.so_ext) have_module = os.path.isfile(module_path) need_cythonize = not have_module if args.annotate: html_file = os.path.join(lib_dir, module_name + '.html') if not os.path.isfile(html_file): need_cythonize = True if need_cythonize: c_include_dirs = args.include if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) pyx_file = os.path.join(lib_dir, module_name + '.pyx') pyx_file = py3compat.cast_bytes_py2( pyx_file, encoding=sys.getfilesystemencoding()) with io.open(pyx_file, 'w', encoding='utf-8') as f: f.write(code) extension = Extension( name=module_name, sources=[pyx_file], include_dirs=c_include_dirs, library_dirs=args.library_dirs, extra_compile_args=args.compile_args, extra_link_args=args.link_args, libraries=args.lib, language='c++' if args.cplus else 'c', ) build_extension = self._get_build_extension() try: opts = dict( quiet=quiet, annotate=args.annotate, force=True, ) build_extension.extensions = cythonize([extension], **opts) except CompileError: return if not have_module: build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module) if args.annotate: try: with io.open(html_file, encoding='utf-8') as f: annotated_html = f.read() except IOError as e: # File could not be opened. Most likely the user has a version # of Cython before 0.15.1 (when `cythonize` learned the # `force` keyword argument) and has already compiled this # exact source without annotation. print( 'Cython completed successfully but the annotated ' 'source could not be read.', file=sys.stderr) print(e, file=sys.stderr) else: return display.HTML(self.clean_annotated_html(annotated_html))
def test_encourage_iframe_over_html(m_warn): display.HTML('<br />') m_warn.assert_not_called() display.HTML('<iframe src="http://a.com"></iframe>') m_warn.assert_called_with('Consider using IPython.display.IFrame instead')
def cython(self, line, cell): """Compile and import everything from a Cython code cell. The contents of the cell are written to a `.pyx` file in the directory `IPYTHONDIR/cython` using a filename with the hash of the code. This file is then cythonized and compiled. The resulting module is imported and all of its symbols are injected into the user's namespace. The usage is similar to that of `%%cython_pyximport` but you don't have to pass a module name:: %%cython def f(x): return 2.0*x To compile OpenMP codes, pass the required `--compile-args` and `--link-args`. For example with gcc:: %%cython --compile-args=-fopenmp --link-args=-fopenmp ... To enable profile guided optimisation, pass the ``--pgo`` option. Note that the cell itself needs to take care of establishing a suitable profile when executed. This can be done by implementing the functions to optimise, and then calling them directly in the same cell on some realistic training data like this:: %%cython --pgo def critical_function(data): for item in data: ... # execute function several times to build profile from somewhere import some_typical_data for _ in range(100): critical_function(some_typical_data) In Python 3.5 and later, you can distinguish between the profile and non-profile runs as follows:: if "_pgo_" in __name__: ... # execute critical code here """ args = magic_arguments.parse_argstring(self.cython, line) code = cell if cell.endswith('\n') else cell + '\n' lib_dir = os.path.join(get_ipython_cache_dir(), 'cython') key = (code, line, sys.version_info, sys.executable, cython_version) if not os.path.exists(lib_dir): os.makedirs(lib_dir) if args.pgo: key += ('pgo',) if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += (time.time(),) if args.name: module_name = py3compat.unicode_to_str(args.name) else: module_name = "_cython_magic_" + hashlib.sha1(str(key).encode('utf-8')).hexdigest() html_file = os.path.join(lib_dir, module_name + '.html') module_path = os.path.join(lib_dir, module_name + self.so_ext) have_module = os.path.isfile(module_path) need_cythonize = args.pgo or not have_module if args.annotate: if not os.path.isfile(html_file): need_cythonize = True extension = None if need_cythonize: extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet) assert len(extensions) == 1 extension = extensions[0] self._code_cache[key] = module_name if args.pgo: self._profile_pgo_wrapper(extension, lib_dir) self._build_extension(extension, lib_dir, pgo_step_name='use' if args.pgo else None, quiet=args.quiet) module = imp.load_dynamic(module_name, module_path) self._import_all(module) if args.annotate: try: with io.open(html_file, encoding='utf-8') as f: annotated_html = f.read() except IOError as e: # File could not be opened. Most likely the user has a version # of Cython before 0.15.1 (when `cythonize` learned the # `force` keyword argument) and has already compiled this # exact source without annotation. print('Cython completed successfully but the annotated ' 'source could not be read.', file=sys.stderr) print(e, file=sys.stderr) else: return display.HTML(self.clean_annotated_html(annotated_html))
def display_file(file_name): with open(file_name, 'r') as f: file = f.read() idisplay.display(idisplay.HTML(f'<pre>{file}</pre>'))
def display_html(html): idisplay.display(idisplay.HTML(html))
def display_html(html_str): import IPython.core.display as IPyDisp IPyDisp.display(IPyDisp.HTML(str(html_str)))
def init(): result = display.HTML(load_js_dependecies()) return result