Quick Python Print
This repo is inspired by "Python Quick Print". "Python Quick Print" can quickly print out variables on the console by using shortcut Ctrl+Shift+L
, while "Quick Python Print" enhance some features:
- You can put the cursor at target line, the extension will automatically recognize the variable in the line.
- Press
Ctrl+Shift+O
to print out tensor shape, e.g.print("==>> a.shape: ", a.shape)
. - Press
Ctrl+Shift+T
to print out variable type, e.g.print("==>> type(a): ", type(a))
. - Press
Ctrl+Shift+;
to insertprint("".center(50, "-"))
used for printing separator line. - Move the cursor inside the bracket if insert simply
print()
. - Press
Ctrl+Shift+/
to comment out or uncomment all print statement in the scope. - Press
Ctrl+Shift+R
to delete all print statements in the scope. - Allow user to define customized prefix and suffix of print content flexibly in extension settings.
- Able to output colored text in terminal by using python built-in package:
termcolor
.
If you find bug or have feature request, you can check out the ChangeLog to see if this bug or feature had been fixed or added in latest version, if not, welcome to post in this extension's github repo.
Motivation
When I am learning deep learning model, I often want to know how the tensor shape changes along the way, I think this will help me understand how the deep learning model works. Take pytorh for example, these operations view,slice,concat,permute,conv,linear etc
all will change the tensor shape. Fortunately, many deep learning framework all have .shape
attribute of tensor(as far as I know, there are pytorch, numpy, tensorflow, paddlepaddle, oneflow), so this makes the extension be useful for different deep learning framework users, afterwards, I add more features to make it also useful for other python developer.
Installation
Before installation, make sure your vscode version ≥ 1.57.0. This extension is available in the Visual Studio Code Extension Marketplace, you can search "Quick-Python-Print" and install it.
How to use
Introduction video: Bilibili | Youtube (When I made this video, it was version 0.1.1, and there have been many changes in the current version)
This extension only activates within .py
and .ipynb
files
Keyboard Shortcut
For Mac user, the Ctrl
should be Cmd
.
Ctrl+Shift+L
Select the variable, then press Ctrl+Shift+L
, it will insert print statement at next line:
You can also just put the cursor at the target line, then the extension will automatically recognize the variable:
If you didn't select variable or the extension can't recognize variable, it will just insert simply print() and move the cursor inside it. If selected variable is new defined for the first time, insert code at current line :
Ctrl+Shift+O
Ctl+Shift+O
is similar to Ctrl+Shift+L
except that it will print tensor shape by default:
Ctrl+Shift+T
Ctl+Shift+T
is similar to above except that it will print type of variable by default:
Ctrl+Shift+/
Comment out or uncomment the print statement in the python file or in the selected scope:
Ctrl+Shift+up
Comment out all print statement above the cursor in the python file:
Ctrl+Shift+down
Comment out all print statement below the cursor in the python file:
Ctrl+Shift+;
Insert a print statement that will print 50 -
to become a delimiter like --------------------------------------------------
:
Ctrl+Shift+R
Delete all the print statement(include those commented) in the python file or in the selected scope::
Compatibility with different framework
Many deep learning framework all have .shape
attribute of tensor:
If any of these shortcuts conflicts with existing shortcut, you may change it in the Keyboard Shortcuts Setting
: Press Cmd+P
or Ctrl+P
and type in >Open Keyboard Shortcuts
. Search for the conflict shortcut, then you will find those shortcut using the same keys, then right click one of them, modify or delete keybinding.
Extension Settings
This extension has following settings:
You can go to the Extension Settings
to modify them to suit your preferences. After modification, it should take effect immediately.
Color output text
To color the output text in terminal, you need to do these things:
- Go to
Extension Settings
, check the5.enable-colored-output-text
to be true. And you can select the color you like from the drop-down list. - Add
from termcolor import colored
in the python file - Now Press
Ctrl+Shift+L
orCtrl+Shift+O
orCtrl+Shift+T
will insert the print statement that can color output text. - Run Python File in Terminal.
More
I also make a pypi package : printensor to uppack tensors inside list, tuple, dict, generator, then print their tensor shape. After installing and import, you can replace print(
with prints(
to intergrate with this extension.
Known issue
This extension can not handle tensor that cross multiple lines, for example:
a = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]])
You can use Alt + down
to move down the inserted code.
Warning
This uses Python 3 syntax,If you're using Python 2 print isn't a function.You can import this behavior from __future__
: from __future__ import print_function
Donation
If you find this extension can help you save time, and willing to donate me, i would be very grateful !
License
MIT License