How can I go to a function's definition in Jupyter notebook? - python

How can i go to a function definition in Jupyter?
I want something like Visual studio's f12, or eclipse's and PyCharm's ctrl+click.
I find it hard to believe this does not exist, yet couldn't find it

I'm not aware of such feature that will work for all kernels.
If you are using a Python kernel and have ipython installed you can use inspection functions:
%pdoc <object>: Print (or run through a pager if too long) the docstring for an object. If the given object is a class, it will print
both the class and theconstructor docstrings.
%pdef <object>: Print the call signature for any callable object. If the object is a class, print the constructor information.
%psource <object>: Print (or run through a pager if too long) the source code for an object.
%pfile <object>: Show the entire source file where an object was defined via a pager, opening it at the line where the object
definition begins.
%who/%whos: These functions give information about identifiers you have defined interactively (not things you loaded or defined in
your configuration files). %who just prints a list of identifiers and
%whos prints a table with some basic details about each identifier.
Typing ??word or word?? gives access to the full information,
including the source code where possible. Long strings are not
snipped.
Usage Example
In [4]: pd.DataFrame?
In [5]: pd.DataFrame??
In [6]: %pdef pd.Dataframe
Object `pd.Dataframe` not found.
In [7]: %pdef pd.DataFrame
Class constructor information:
pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
In [8]: %pdoc pd.DataFrame
In [9]: %pfile pd.DataFrame
Resources
Dynamic object information

If you can upgrade to JupyterLab, you could install jupyterlab-go-to-definition extension with:
jupyter labextension install #krassowski/jupyterlab_go_to_definition
It supports R and Python and allows to jump to definitions with alt-click (where alt can be changed to another key modifier in the settings). For more usage details see the linked GitHub repository (above).
On the technical side, there are two ways to get the location of definitions in a notebook: static analysis and inspection in the kernel. If you cannot upgrade to JupyterLab yet, you could try to re-implement either of those for jupyter-notebook.
The kernel inspections may seem easier to implement because they do not depend on the code editor used, but have to be written for each language separately. Here is an example for Python
static analysis can be done via external tool but those will be problematic due to the nature of the notebook (being composed of multiple cells, unlike normal source code files); the approach used in the extension above is to analyze the tokens of CodeMirror editor, iterating over cells, starting from the cell with the symbol usage (where the user alt-clicked on a variable). Here is an example of implementation for such iteration.
Disclaimer: I am the author of this extension.
Edit: For an implementation which allows the same for any language (provided that you install a language server for this language), see: jupyterlab-lsp (with many other features too!). The difference is that it only uses static analysis for now (the kernel-based position retrieval is not implemented just yet).

I used below to return call signature of function, taken from above
np.matmul??
If you have the function typed out, you can use keyboard combination 'Shift+Tab' + 'Shift+Tab' to get tool tip in notebook when in edit mode, as shown below.

Related

Can I tell pylance in VS Code to assume import of certain symbols globally for all files?

I am working with code that Amazon's boto3 library. That library does not define any symbols normally and generates lot of the classes at runtime. As a result, it is not possible to use it for type hints.
There is a library called boto3-stubs, I found it in this answer: https://stackoverflow.com/a/54676985/607407 However this is a large codebase and there is certain inertia to adding new libraries, especially ones that do not actually do anything.
What I can do is use forward type annotations, like this:
def fn(versions: Iterator['ObjectVersion']):
pass
This will not affect the runtime and the rest of our team is ok with these names. But it serves little purpose unless pylance can recognize that I mean mypy_boto3_s3.service_resource.ObjectVersion.
Is there a setting that can tell Pylance: "For every file, automatically import these modules."?

Visual Studio Code Does Not Suggest Python Object Attributes

I use VS Code v1.25.1 with the Python extension added. Although it suggests me the attributes and methods of libraries and classes, i.e. I type from sklearn. it makes suggestions like ensemble, exception etc., when I generate a numpy array x_data = np.linspace(0.0, 10.0, 1000000) and on the next line say I would like to use a method or an attribute such as argmax it does not show me the suggestion list after typing x_data..
In Jupyter Notebook I used to press tab after typing x_data. to see the list. I wonder if there is any shortcut to achieve the same.
If you have IntelliSense installed, it should work automatically as you type. Or you might try Ctrl + Space. It also depends on your configuration (see the link).

IPython change input cell syntax highlighting logic for entire session

You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.
For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.
How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).
In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.
Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.
I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.
I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?
To replace IPython.config.cell_magic_highlight, you can use something like
import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)
For recent IPython version, the selected answer no longer works. The 'config_default' property was renamed options_default (Ipython 6.0.0).
The following works:
import IPython
js = "IPython.CodeCell.options_default.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)

How do I display function arguments in ipython qtconsole?

I'm using IPython qtconsole under windows 7, and when I first write a method name and type the bracket, a popup shows method parameters.
What is the way to display that popup explicitly, once it has disappeared? This is pretty common 'show method parameters' shortcut that I'm talking about, but I've failed to find the shortcut to it after an embarrassing amount of google searches.
In Spyder, try View - Panes - Object inspector. Then type the full name of the function.
I would highly recommend relying on the Python Library Reference rather than any in-IDE tools, at least for functions and classes that are in the standard library. For objects outside those libraries however... it looks like you can type object_name followed by a question mark, that is, object_name?, to get a list of informative details about the object. (Since everything is an object, this presumably includes functions.)
For your specific question, it looks, from the iPython docs, like the TAB key is what you're looking for, but somehow I doubt you haven't already tried that.

Running unexported .dll functions with python

This may seem like a weird question, but I would like to know how I can run a function in a .dll from a memory 'signature'. I don't understand much about how it actually works, but I needed it badly. Its a way of running unexported functions from within a .dll, if you know the memory signature and adress of it.
For example, I have these:
respawn_f "_ZN9CCSPlayer12RoundRespawnEv"
respawn_sig "568BF18B06FF90B80400008B86E80D00"
respawn_mask "xxxxx?xxx??xxxx?"
And using some pretty nifty C++ code you can use this to run functions from within a .dll.
Here is a well explained article on it:
http://wiki.alliedmods.net/Signature_Scanning
So, is it possible using Ctypes or any other way to do this inside python?
If you can already run them using C++ then you can try using SWIG to generate python wrappers for the C++ code you've written making it callable from python.
http://www.swig.org/
Some caveats that I've found using SWIG:
Swig looks up types based on a string value. For example
an integer type in Python (int) will look to make sure
that the cpp type is "int" otherwise swig will complain
about type mismatches. There is no automatic conversion.
Swig copies source code verbatim therefore even objects in the same namespace
will need to be fully qualified so that the cxx file will compile properly.
Hope that helps.
You said you were trying to call a function that was not exported; as far as I know, that's not possible from Python. However, your problem seems to be merely that the name is mangled.
You can invoke an arbitrary export using ctypes. Since the name is mangled, and isn't a valid Python identifier, you can use getattr().
Another approach if you have the right information is to find the export by ordinal, which you'd have to do if there was no name exported at all. One way to get the ordinal would be using dumpbin.exe, included in many Windows compiled languages. It's actually a front-end to the linker, so if you have the MS LinK.exe, you can also use that with appropriate commandline switches.
To get the function reference (which is a "function-pointer" object bound to the address of it), you can use something like:
import ctypes
func = getattr(ctypes.windll.msvcrt, "##myfunc")
retval = func(None)
Naturally, you'd replace the 'msvcrt' with the dll you specifically want to call.
What I don't show here is how to unmangle the name to derive the calling signature, and thus the arguments necessary. Doing that would require a demangler, and those are very specific to the brand AND VERSION of C++ compiler used to create the DLL.
There is a certain amount of error checking if the function is stdcall, so you can sometimes fiddle with things till you get them right. But if the function is cdecl, then there's no way to automatically check. Likewise you have to remember to include the extra this parameter if appropriate.

Categories