Python IPython.core.magic_arguments.magic_arguments() Examples
The following are 24
code examples of IPython.core.magic_arguments.magic_arguments().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
IPython.core.magic_arguments
, or try the search function
.
Example #1
Source File: script.py From Computable with MIT License | 6 votes |
def _make_script_magic(self, name): """make a named magic, that calls %%script with a particular program""" # expand to explicit path if necessary: script = self.script_paths.get(name, name) @magic_arguments.magic_arguments() @script_args def named_script_magic(line, cell): # if line, add it as cl-flags if line: line = "%s %s" % (script, line) else: line = script return self.shebang(line, cell) # write a basic docstring: named_script_magic.__doc__ = \ """%%{name} script magic Run cells with {script} in a subprocess. This is a shortcut for `%%script {script}` """.format(**locals()) return named_script_magic
Example #2
Source File: heat.py From pyheatmagic with MIT License | 6 votes |
def heat(self, line, cell): """Method to profile the python code in the ipython cell and display it as a heatmap using py-heat package. :param line: Line value for the ipython line this magic is called from. :param cell: Cell value for the ipython cell this magic is called from. """ args = magic_arguments.parse_argstring(self.heat, line) filename = args.out if filename is not None: filename = os.path.expanduser(args.out) _, tmp_file = mkstemp() with open(tmp_file, "wb") as f: f.write(cell.encode()) pyheat = PyHeat(tmp_file) pyheat.create_heatmap() pyheat.show_heatmap(output_file=filename) pyheat.close_heatmap() os.remove(tmp_file)
Example #3
Source File: osm.py From Computable with MIT License | 6 votes |
def writefile(self, line, cell): """Write the contents of the cell to a file. The file will be overwritten unless the -a (--append) flag is specified. """ args = magic_arguments.parse_argstring(self.writefile, line) filename = os.path.expanduser(unquote_filename(args.filename)) if os.path.exists(filename): if args.append: print "Appending to %s" % filename else: print "Overwriting %s" % filename else: print "Writing %s" % filename mode = 'a' if args.append else 'w' with io.open(filename, mode, encoding='utf-8') as f: f.write(cell)
Example #4
Source File: basic.py From Computable with MIT License | 5 votes |
def notebook(self, s): """Export and convert IPython notebooks. This function can export the current IPython history to a notebook file or can convert an existing notebook file into a different format. For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". To export the history to "foo.py" do "%notebook -e foo.py". To convert "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible formats include (json/ipynb, py). """ args = magic_arguments.parse_argstring(self.notebook, s) from IPython.nbformat import current args.filename = unquote_filename(args.filename) if args.export: fname, name, format = current.parse_filename(args.filename) cells = [] hist = list(self.shell.history_manager.get_range()) for session, prompt_number, input in hist[:-1]: cells.append(current.new_code_cell(prompt_number=prompt_number, input=input)) worksheet = current.new_worksheet(cells=cells) nb = current.new_notebook(name=name,worksheets=[worksheet]) with io.open(fname, 'w', encoding='utf-8') as f: current.write(nb, f, format); elif args.format is not None: old_fname, old_name, old_format = current.parse_filename(args.filename) new_format = args.format if new_format == u'xml': raise ValueError('Notebooks cannot be written as xml.') elif new_format == u'ipynb' or new_format == u'json': new_fname = old_name + u'.ipynb' new_format = u'json' elif new_format == u'py': new_fname = old_name + u'.py' else: raise ValueError('Invalid notebook format: %s' % new_format) with io.open(old_fname, 'r', encoding='utf-8') as f: nb = current.read(f, old_format) with io.open(new_fname, 'w', encoding='utf-8') as f: current.write(nb, f, new_format)
Example #5
Source File: jupyter_slack.py From jupyter-slack-notify with MIT License | 5 votes |
def notify(self, line="", cell=None): args = magic_arguments.parse_argstring(self.notify, line) mess = args.message.replace("\"", "") with Monitor(mess, time=args.time): self.shell.ex(cell)
Example #6
Source File: ipython.py From storlets with Apache License 2.0 | 5 votes |
def put(self, line): args = magic_arguments.parse_argstring(self.put, line) if not args.o: raise UsageError('-o option is mandatory for the invocation') if not args.o[0].startswith(tuple(string.ascii_letters)): raise UsageError('The output variable name must be a valid prefix ' 'of a python variable, that is, start with a ' 'letter') if not args.storlet: raise UsageError('--storlet option is mandatory ' 'for the invocation') if not args.input: raise UsageError('--input option is mandatory for the invocation') if not args.input.startswith('/'): raise UsageError('--input argument must be a full path') if not args.output: raise UsageError('--output option is mandatory for the invocation') dst_container, dst_obj = self._parse_input_path(args.output) headers = {'X-Run-Storlet': '%s' % args.storlet} # pick -i option and translate the params to # X-Storlet-Parameter-x headers storlet_headers = self._generate_params_headers( self.shell.user_ns[args.i] if args.i else {}) headers.update(storlet_headers) # invoke storlet app on copy conn = get_swift_connection() response_dict = dict() with open(args.input, 'rb') as content: conn.put_object( dst_container, dst_obj, content, headers=headers, response_dict=response_dict) res = Response(int(response_dict['status']), response_dict['headers']) self.shell.user_ns[args.o] = res
Example #7
Source File: ipython.py From storlets with Apache License 2.0 | 5 votes |
def copy(self, line): args = magic_arguments.parse_argstring(self.copy, line) if not args.o: raise UsageError('-o option is mandatory for the invocation') if not args.o[0].startswith(tuple(string.ascii_letters)): raise UsageError('The output variable name must be a valid prefix ' 'of a python variable, that is, start with a ' 'letter') if not args.storlet: raise UsageError('--storlet option is mandatory ' 'for the invocation') if not args.input: raise UsageError('--input option is mandatory for the invocation') if not args.output: raise UsageError('--output option is mandatory for the invocation') src_container, src_obj = self._parse_input_path(args.input) dst_container, dst_obj = self._parse_input_path(args.output) destination = '/%s/%s' % (dst_container, dst_obj) headers = {'X-Run-Storlet': '%s' % args.storlet} # pick -i option and translate the params to # X-Storlet-Parameter-x headers storlet_headers = self._generate_params_headers( self.shell.user_ns[args.i] if args.i else {}) headers.update(storlet_headers) # invoke storlet app on copy conn = get_swift_connection() response_dict = dict() conn.copy_object( src_container, src_obj, destination=destination, headers=headers, response_dict=response_dict) res = Response(int(response_dict['status']), response_dict['headers']) self.shell.user_ns[args.o] = res
Example #8
Source File: ipython.py From storlets with Apache License 2.0 | 5 votes |
def get(self, line): args = magic_arguments.parse_argstring(self.get, line) if not args.o: raise UsageError('-o option is mandatory for the invocation') if not args.o[0].startswith(tuple(string.ascii_letters)): raise UsageError('The output variable name must be a valid prefix ' 'of a python variable, that is, start with a ' 'letter') if not args.storlet: raise UsageError('--storlet option is mandatory ' 'for the invocation') if not args.input: raise UsageError('--input option is mandatory for the invocation') src_container, src_obj = self._parse_input_path(args.input) headers = {'X-Run-Storlet': '%s' % args.storlet} # pick -i option and translate the params to # X-Storlet-Parameter-x headers storlet_headers = self._generate_params_headers( self.shell.user_ns[args.i] if args.i else {}) headers.update(storlet_headers) # invoke storlet app on get conn = get_swift_connection() response_dict = dict() resp_headers, resp_content_iter = conn.get_object( src_container, src_obj, resp_chunk_size=64 * 1024, headers=headers, response_dict=response_dict) res = Response(int(response_dict['status']), resp_headers, resp_content_iter) self.shell.user_ns[args.o] = res
Example #9
Source File: ipython.py From storlets with Apache License 2.0 | 5 votes |
def uploadfile(self, line, cell): """Upload the contents of the cell to OpenStack Swift. """ args = magic_arguments.parse_argstring(self.uploadfile, line) container, obj = args.container_obj.split('/', 1) conn = get_swift_connection() conn.put_object(container, obj, cell, {'Content-Type': 'application/python'})
Example #10
Source File: blackcellmagic.py From blackcellmagic with MIT License | 5 votes |
def black(self, line, cell): """Magic command to format the IPython cell.""" args = magic_arguments.parse_argstring(self.black, line) line_length = args.line_length if cell: try: from black import FileMode mode = FileMode(line_length=line_length) formatted = format_str(src_contents=cell, mode=mode) except TypeError: formatted = format_str(src_contents=cell, line_length=line_length) if formatted and formatted[-1] == "\n": formatted = formatted[:-1] self.shell.set_next_input(formatted, replace=True)
Example #11
Source File: _system_commands.py From colabtools with Apache License 2.0 | 5 votes |
def _shell_cell_magic(args, cmd): """Run the cell via a shell command, allowing input to be provided. Also available as a line magic. Usage: # Returns a ShellResult. %%shell echo "hello" This is similar to Jupyter's `!` magic, but additionally allows input to be provided to the subprocess. By default, if the subprocess returns a non-zero exit code a `subprocess.CalledProcessError` is raised. The provided command is run within a bash shell. Args: args: Optional arguments. cmd: The shell command to execute. Returns: ShellResult containing the results of the executed command. Raises: subprocess.CalledProcessError: If the subprocess exited with a non-zero exit code and the `ignore_errors` argument wasn't provided. """ parsed_args = magic_arguments.parse_argstring(_shell_cell_magic, args) result = _run_command(cmd, clear_streamed_output=False) if not parsed_args.ignore_errors: result.check_returncode() return result
Example #12
Source File: _magics.py From altair with BSD 3-Clause "New" or "Revised" License | 5 votes |
def vegalite(line, cell): """Cell magic for displaying vega-lite visualizations in CoLab. %%vegalite [dataframe] [--json] [--version=3] Visualize the contents of the cell using Vega-Lite, optionally specifying a pandas DataFrame object to be used as the dataset. if --json is passed, then input is parsed as json rather than yaml. """ args = magic_arguments.parse_argstring(vegalite, line) version = args.version assert version in RENDERERS["vega-lite"] VegaLite = RENDERERS["vega-lite"][version] data_transformers = TRANSFORMERS["vega-lite"][version] if args.json: spec = json.loads(cell) elif not YAML_AVAILABLE: try: spec = json.loads(cell) except json.JSONDecodeError: raise ValueError( "%%vegalite: spec is not valid JSON. " "Install pyyaml to parse spec as yaml" ) else: spec = yaml.load(cell, Loader=yaml.FullLoader) if args.data is not None: data = _get_variable(args.data) spec["data"] = _prepare_data(data, data_transformers) return VegaLite(spec)
Example #13
Source File: sectools_magics.py From msticpy with MIT License | 5 votes |
def b64(self, line: str = "", cell: str = None) -> str: """ Base64 IPython magic extension. Parameters ---------- line : str, optional Line contents, by default "" cell : str, optional Cell contents, by default None Returns ------- str Decoded text """ if cell is None: results, df_results = base64.unpack(line) else: results, df_results = base64.unpack(cell) args = magic_arguments.parse_argstring(self.b64, line) if args.clean: results = re.sub(self._STRIP_TAGS, "", results) elif args.pretty: if _BS_AVAILABLE: xml_str = f"<decoded_string>{results}</decoded_string>" b_soup = BeautifulSoup(xml_str, "xml") results = b_soup.prettify() if args.out is not None: self.shell.user_ns[args.out] = (results, df_results) return results
Example #14
Source File: pylab.py From Computable with MIT License | 5 votes |
def matplotlib(self, line=''): """Set up matplotlib to work interactively. This function lets you activate matplotlib interactive support at any point during an IPython session. It does not import anything into the interactive namespace. If you are using the inline matplotlib backend for embedded figures, you can adjust its behavior via the %config magic:: # enable SVG figures, necessary for SVG+XHTML export in the qtconsole In [1]: %config InlineBackend.figure_format = 'svg' # change the behavior of closing all figures at the end of each # execution (cell), or allowing reuse of active figures across # cells: In [2]: %config InlineBackend.close_figures = False Examples -------- In this case, where the MPL default is TkAgg:: In [2]: %matplotlib Using matplotlib backend: TkAgg But you can explicitly request a different backend:: In [3]: %matplotlib qt """ args = magic_arguments.parse_argstring(self.matplotlib, line) gui, backend = self.shell.enable_matplotlib(args.gui) self._show_matplotlib_backend(args.gui, backend)
Example #15
Source File: execution.py From Computable with MIT License | 5 votes |
def capture(self, line, cell): """run the cell, capturing stdout/err""" args = magic_arguments.parse_argstring(self.capture, line) out = not args.no_stdout err = not args.no_stderr with capture_output(out, err) as io: self.shell.run_cell(cell) if args.output: self.shell.user_ns[args.output] = io
Example #16
Source File: execution.py From Computable with MIT License | 5 votes |
def debug(self, line='', cell=None): """Activate the interactive debugger. This magic command support two ways of activating debugger. One is to activate debugger before executing code. This way, you can set a break point, to step through the code from the point. You can use this mode by giving statements to execute and optionally a breakpoint. The other one is to activate debugger in post-mortem mode. You can activate this mode simply running %debug without any argument. If an exception has just occurred, this lets you inspect its stack frames interactively. Note that this will always work only on the last traceback that occurred, so you must call this quickly after an exception that you wish to inspect has fired, because if another one occurs, it clobbers the previous one. If you want IPython to automatically do this on every exception, see the %pdb magic for more details. """ args = magic_arguments.parse_argstring(self.debug, line) if not (args.breakpoint or args.statement or cell): self._debug_post_mortem() else: code = "\n".join(args.statement) if cell: code += "\n" + cell self._debug_exec(code, args.breakpoint)
Example #17
Source File: magics.py From Computable with MIT License | 5 votes |
def result(self, line=''): """Print the result of the last asynchronous %px command. This lets you recall the results of %px computations after asynchronous submission (block=False). Examples -------- :: In [23]: %px os.getpid() Async parallel execution on engine(s): all In [24]: %pxresult Out[8:10]: 60920 Out[9:10]: 60921 Out[10:10]: 60922 Out[11:10]: 60923 """ args = magic_arguments.parse_argstring(self.result, line) if self.last_result is None: raise UsageError(NO_LAST_RESULT) self.last_result.get() self.last_result.display_outputs(groupby=args.groupby)
Example #18
Source File: magics.py From Computable with MIT License | 5 votes |
def pxconfig(self, line): """configure default targets/blocking for %px magics""" args = magic_arguments.parse_argstring(self.pxconfig, line) if args.targets: self.view.targets = self._eval_target_str(args.targets) if args.block is not None: self.view.block = args.block if args.set_verbose is not None: self.verbose = args.set_verbose
Example #19
Source File: magics.py From Computable with MIT License | 5 votes |
def output_args(f): """decorator for output-formatting args applied to %pxresult and %%px """ args = [ magic_arguments.argument('-r', action="store_const", dest='groupby', const='order', help="collate outputs in order (same as group-outputs=order)" ), magic_arguments.argument('-e', action="store_const", dest='groupby', const='engine', help="group outputs by engine (same as group-outputs=engine)" ), magic_arguments.argument('--group-outputs', dest='groupby', type=str, choices=['engine', 'order', 'type'], default='type', help="""Group the outputs in a particular way. Choices are: type: group outputs of all engines by type (stdout, stderr, displaypub, etc.). engine: display all output for each engine together. order: like type, but individual displaypub output from each engine is collated. For example, if multiple plots are generated by each engine, the first figure of each engine will be displayed, then the second of each, etc. """ ), magic_arguments.argument('-o', '--out', dest='save_name', type=str, help="""store the AsyncResult object for this computation in the global namespace under this name. """ ), ] for a in args: f = a(f) return f
Example #20
Source File: magics.py From Computable with MIT License | 5 votes |
def exec_args(f): """decorator for adding block/targets args for execution applied to %pxconfig and %%px """ args = [ magic_arguments.argument('-b', '--block', action="store_const", const=True, dest='block', help="use blocking (sync) execution", ), magic_arguments.argument('-a', '--noblock', action="store_const", const=False, dest='block', help="use non-blocking (async) execution", ), magic_arguments.argument('-t', '--targets', type=str, help="specify the targets on which to execute", ), magic_arguments.argument('--local', action="store_const", const=True, dest="local", help="also execute the cell in the local namespace", ), magic_arguments.argument('--verbose', action="store_const", const=True, dest="set_verbose", help="print a message at each execution", ), magic_arguments.argument('--no-verbose', action="store_const", const=False, dest="set_verbose", help="don't print any messages", ), ] for a in args: f = a(f) return f
Example #21
Source File: sectools_magics.py From msticpy with MIT License | 5 votes |
def ioc(self, line="", cell=None) -> List[Tuple[str, List[str]]]: """ Ioc Extract IPython magic extension. Parameters ---------- line : str, optional Line contents, by default "" cell : str, optional Cell contents, by default None Returns ------- List[Tuple[str, List[str]]] List of tuples of IoCs found grouped by type. """ args = magic_arguments.parse_argstring(self.ioc, line) ioc_types = None if args.ioc_types: ioc_types = [ioc_type.strip() for ioc_type in args.ioc_types.split(",")] if cell is None: results = self._ioc_extract.extract(src=line, ioc_types=ioc_types) else: results = self._ioc_extract.extract(src=cell, ioc_types=ioc_types) iocs = [(ioc_type, list(ioc_res)) for ioc_type, ioc_res in results.items()] if args.out is not None: self.shell.user_ns[args.out] = results return iocs
Example #22
Source File: _magics.py From altair with BSD 3-Clause "New" or "Revised" License | 4 votes |
def vega(line, cell): """Cell magic for displaying Vega visualizations in CoLab. %%vega [name1:variable1 name2:variable2 ...] [--json] [--version='5'] Visualize the contents of the cell using Vega, optionally specifying one or more pandas DataFrame objects to be used as the datasets. If --json is passed, then input is parsed as json rather than yaml. """ args = magic_arguments.parse_argstring(vega, line) version = args.version assert version in RENDERERS["vega"] Vega = RENDERERS["vega"][version] data_transformers = TRANSFORMERS["vega"][version] def namevar(s): s = s.split(":") if len(s) == 1: return s[0], s[0] elif len(s) == 2: return s[0], s[1] else: raise ValueError("invalid identifier: '{}'".format(s)) try: data = list(map(namevar, args.data)) except ValueError: raise ValueError("Could not parse arguments: '{}'".format(line)) if args.json: spec = json.loads(cell) elif not YAML_AVAILABLE: try: spec = json.loads(cell) except json.JSONDecodeError: raise ValueError( "%%vega: spec is not valid JSON. " "Install pyyaml to parse spec as yaml" ) else: spec = yaml.load(cell, Loader=yaml.FullLoader) if data: spec["data"] = [] for name, val in data: val = _get_variable(val) prepped = _prepare_data(val, data_transformers) prepped["name"] = name spec["data"].append(prepped) return Vega(spec)
Example #23
Source File: magics.py From Computable with MIT License | 4 votes |
def cell_px(self, line='', cell=None): """Executes the cell in parallel. Examples -------- :: In [24]: %%px --noblock ....: a = os.getpid() Async parallel execution on engine(s): all In [25]: %%px ....: print a [stdout:0] 1234 [stdout:1] 1235 [stdout:2] 1236 [stdout:3] 1237 """ args = magic_arguments.parse_argstring(self.cell_px, line) if args.targets: save_targets = self.view.targets self.view.targets = self._eval_target_str(args.targets) # if running local, don't block until after local has run block = False if args.local else args.block try: ar = self.parallel_execute(cell, block=block, groupby=args.groupby, save_name=args.save_name, ) finally: if args.targets: self.view.targets = save_targets # run locally after submitting remote block = self.view.block if args.block is None else args.block if args.local: self.shell.run_cell(cell) # now apply blocking behavor to remote execution if block: ar.get() ar.display_outputs(args.groupby) if not block: return ar
Example #24
Source File: ipython.py From storlets with Apache License 2.0 | 4 votes |
def storletapp(self, line, cell): args = magic_arguments.parse_argstring(self.storletapp, line) module_path = args.module_class assert module_path.count('.') == 1 headers = { 'X-Object-Meta-Storlet-Language': 'python', 'X-Object-Meta-Storlet-Interface-Version': '1.0', 'X-Object-Meta-Storlet-Object-Metadata': 'no', 'X-Object-Meta-Storlet-Main': module_path, 'Content-Type': 'application/octet-stream', } storlet_obj = '%s.py' % module_path.split('.')[0] conn = get_swift_connection() conn.put_object(args.container, storlet_obj, cell, headers=headers) print('Upload storlets succeeded /%s/%s' % (args.container, storlet_obj)) print('Example command `swift download <container> <object> ' '-H X-Run-Storlet:%s`' % storlet_obj) if args.with_invoke: if not args.input: raise UsageError( '--with-invoke option requires --input to run the app') src_container, src_obj = self._parse_input_path(args.input) headers = {'X-Run-Storlet': '%s' % storlet_obj} # invoke storlet app resp_headers, resp_content_iter = conn.get_object( src_container, src_obj, resp_chunk_size=64 * 1024, headers=headers) print('Invocation Complete') if args.print_result: print('Result Content:') print(''.join(resp_content_iter)) else: # drain all resp content stream for x in resp_content_iter: pass