Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print code link into PyCharms console

I'm currently writing some complex tests for our Django-Application in PyCharm. Therefore I'm using all those fancy colors on the console, which can be achieved by printing some special codes.

I'm now wondering whether it is also possible to print hyperlinks to specific code lines in the console. For every stacktrace PyCharm generates for all files such links, but is it possible to do that on my own?

That would be very helpful, to jump to the specific lines directly from the test output, instead of manually navigate to.

like image 200
Gnietschow Avatar asked Oct 10 '14 13:10

Gnietschow


2 Answers

I guess this is a little late and I am not 100% happy with it, but it just works (at least with PyCharm 2019.2). If you print out a line in the format of

File "{file_path}", line {line_number}

then PyCharm will automatically convert this line into a link to the corresponding file and line. For example:

print("File \"C:/Workspace/enventa-next/suites/suite_standard/tst_Inventur/test.py\", line 3")

Will jump to or open the file and move focus to the third line. I think a link with custom content is not possible.

like image 128
Frieder Avatar answered Nov 11 '22 11:11

Frieder


Function: print_link(file, line)

With the help of Frieder's answer I created print_link which allows you to easily create a link in your PyCharm console. It uses the built-in inspect module to create default values for file and line to point to the line that called it.

import inspect

def print_link(file=None, line=None):
    """ Print a link in PyCharm to a line in file.
        Defaults to line where this function was called. """
    if file is None:
        file = inspect.stack()[1].filename
    if line is None:
        line = inspect.stack()[1].lineno
    string = f'File "{file}", line {max(line, 1)}'.replace("\\", "/")
    print(string)
    return string
# Default to this line and file (randomtesting.py in generallibrary, line 14)
print_link()

# Link relatively to another repository through parent's directory
print_link("../generalvector/generalvector/vector.py", 23)

# Link relatively to README which is in same directory as randomtesting.py
print_link("README.md", 1)

# Link absolutely to any file
print_link("A:/hello.txt", 1)

print_link

Blue text means that the link is successful.

Function: print_link_to_obj(obj)

Building further on the previous function we can create print_link_to_obj to easily and dynamically create a link to any object which has it's source code available to inspect.

def print_link_to_obj(obj):
    """ Print a link in PyCharm to a module, function, class, method or property. """
    if isinstance(obj, property):
        obj = obj.fget
    file = inspect.getfile(obj)
    line = inspect.getsourcelines(obj)[1]
    print_link(file=file, line=line)
# Create a link to definition above
print_link_to_obj(print_link)

# Create a link to entire inspect module
print_link_to_obj(inspect)

# Create a link to a function in inspect
print_link_to_obj(inspect.stack)

# Create a link to a property in a class in inspect
print_link_to_obj(inspect.BoundArguments.args)

print_link_to_obj

Package: generallibrary PyPI pyversions

Install my cross-platform package if you don't want to copy-paste the functions:

pip install generallibrary
from generallibrary import print_link, print_link_to_obj
like image 27
Mandera Avatar answered Nov 11 '22 09:11

Mandera