-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get variable info with background thread #15495
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b05ca7
retreive variables on background thread
amunger 19673bd
fix range issue
amunger 5b84fba
workaround import restrictions
amunger 407985a
prettier
amunger 2d043ba
register service for web
amunger 3372b4f
get variable summary on background thread
amunger 59c7852
improve naming
amunger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
179 changes: 179 additions & 0 deletions
179
pythonFiles/vscode_datascience_helpers/getVariableInfo/vscodeGetVariablesBackground.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
# Query Jupyter server for the info about a dataframe | ||
from collections import namedtuple | ||
from importlib.util import find_spec | ||
import json | ||
|
||
maxStringLength = 1000 | ||
collectionTypes = ["list", "tuple", "set"] | ||
arrayPageSize = 50 | ||
|
||
|
||
def truncateString(variable): | ||
string = repr(variable) | ||
if len(string) > maxStringLength: | ||
sizeInfo = "\n\nLength: " + str(len(variable)) if type(variable) == str else "" | ||
return string[: maxStringLength - 1] + "..." + sizeInfo | ||
else: | ||
return string | ||
|
||
|
||
DisplayOptions = namedtuple("DisplayOptions", ["width", "max_columns"]) | ||
|
||
|
||
def set_pandas_display_options(display_options=None): | ||
if find_spec("pandas") is not None: | ||
try: | ||
import pandas as _VSCODE_PD | ||
|
||
original_display = DisplayOptions( | ||
width=_VSCODE_PD.options.display.width, | ||
max_columns=_VSCODE_PD.options.display.max_columns, | ||
) | ||
|
||
if display_options: | ||
_VSCODE_PD.options.display.max_columns = display_options.max_columns | ||
_VSCODE_PD.options.display.width = display_options.width | ||
else: | ||
_VSCODE_PD.options.display.max_columns = 100 | ||
_VSCODE_PD.options.display.width = 1000 | ||
|
||
return original_display | ||
except ImportError: | ||
pass | ||
finally: | ||
del _VSCODE_PD | ||
|
||
|
||
def getValue(variable): | ||
original_display = None | ||
if type(variable).__name__ == "DataFrame" and find_spec("pandas") is not None: | ||
original_display = set_pandas_display_options() | ||
|
||
try: | ||
return truncateString(variable=variable) | ||
finally: | ||
if original_display: | ||
set_pandas_display_options(original_display) | ||
|
||
|
||
def getPropertyNames(variable): | ||
props = [] | ||
for prop in dir(variable): | ||
if not prop.startswith("_"): | ||
props.append(prop) | ||
return props | ||
|
||
|
||
def getFullType(varType): | ||
module = "" | ||
if hasattr(varType, "__module__") and varType.__module__ != "builtins": | ||
module = varType.__module__ + "." | ||
if hasattr(varType, "__qualname__"): | ||
return module + varType.__qualname__ | ||
elif hasattr(varType, "__name__"): | ||
return module + varType.__name__ | ||
|
||
|
||
def getVariableDescription(variable): | ||
result = {} | ||
|
||
varType = type(variable) | ||
result["type"] = getFullType(varType) | ||
if hasattr(varType, "__mro__"): | ||
result["interfaces"] = [getFullType(t) for t in varType.__mro__] | ||
|
||
if hasattr(variable, "__len__") and result["type"] in collectionTypes: | ||
result["count"] = len(variable) | ||
|
||
result["hasNamedChildren"] = hasattr(variable, "__dict__") or type(variable) == dict | ||
|
||
result["value"] = getValue(variable) | ||
return result | ||
|
||
|
||
def getChildProperty(root, propertyChain): | ||
try: | ||
variable = root | ||
for property in propertyChain: | ||
if type(property) == int: | ||
if hasattr(variable, "__getitem__"): | ||
variable = variable[property] | ||
elif type(variable) == set: | ||
variable = list(variable)[property] | ||
else: | ||
return None | ||
elif hasattr(variable, property): | ||
variable = getattr(variable, property) | ||
elif type(variable) == dict and property in variable: | ||
variable = variable[property] | ||
else: | ||
return None | ||
except Exception: | ||
return None | ||
|
||
return variable | ||
|
||
|
||
### Get info on variables at the root level | ||
def _VSCODE_getVariableDescriptions(varNames): | ||
variables = [ | ||
{ | ||
"name": varName, | ||
**getVariableDescription(globals()[varName]), | ||
"root": varName, | ||
"propertyChain": [], | ||
"language": "python", | ||
} | ||
for varName in varNames | ||
if varName in globals() | ||
] | ||
|
||
return json.dumps(variables) | ||
|
||
|
||
### Get info on children of a variable reached through the given property chain | ||
def _VSCODE_getAllChildrenDescriptions(rootVarName, propertyChain, startIndex): | ||
root = globals()[rootVarName] | ||
if root is None: | ||
return [] | ||
|
||
parent = root | ||
if len(propertyChain) > 0: | ||
parent = getChildProperty(root, propertyChain) | ||
|
||
children = [] | ||
parentInfo = getVariableDescription(parent) | ||
if "count" in parentInfo: | ||
if parentInfo["count"] > 0: | ||
lastItem = min(parentInfo["count"], startIndex + arrayPageSize) | ||
indexRange = range(startIndex, lastItem) | ||
children = [ | ||
{ | ||
**getVariableDescription(getChildProperty(parent, [i])), | ||
"name": str(i), | ||
"root": rootVarName, | ||
"propertyChain": propertyChain + [i], | ||
"language": "python", | ||
} | ||
for i in indexRange | ||
] | ||
elif parentInfo["hasNamedChildren"]: | ||
childrenNames = [] | ||
if hasattr(parent, "__dict__"): | ||
childrenNames = getPropertyNames(parent) | ||
elif type(parent) == dict: | ||
childrenNames = list(parent.keys()) | ||
|
||
children = [] | ||
for prop in childrenNames: | ||
child_property = getChildProperty(parent, [prop]) | ||
if child_property is not None and type(child_property).__name__ != "method": | ||
child = { | ||
**getVariableDescription(child_property), | ||
"name": prop, | ||
"root": rootVarName, | ||
"propertyChain": propertyChain + [prop], | ||
} | ||
children.append(child) | ||
|
||
return json.dumps(children) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed this PR, and wanted to provide some quick feedback,
We try to avoid injections where possible, as this slows the extension startup.
In this case you can just keep the class, but remove teh `@injectable decorator
& construct the class when you need it, or just pass it from the calling code
Or remove the class entirely and just use the function, I'm assuming you need the class for mocking/testing purposes
Else having this class is not necessary, as it just wraps a function
For instance today we don't use this class in other places, but use the function directly.
Personally prefer to remove this class and keep the function, else there are two ways of doing the same thing
@rebornix /cc thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need it to work around import zone restrictions - the function is in
standalone
(which doesn't seem right to me, but it depends on the API code which is also in that zone), and the caller is in kernel. Neither of those zones can depend on the other.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, then please feel free to move the function out of standalone.
Or leave this as is and I'll fix that later,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for avoiding
injectable
if possible. This is a static helper function that can be optimized by bundlers, making it aninjectable
function doesn't seem to give us any benefit at this moment.Reading the dependencies of
execCodeInBackgroundThread
, it seems we can move it intokernel
component.src\kernels\execution\helpers.ts
might be a good place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that, and you can keep this pr as is . Or feel free to make the change.
I'm fine with either
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed that
const api = createKernelApiForExtension(JVSC_EXTENSION_ID, kernel);
which depends onNotebookKernelExecution#onDidReceiveDisplayUpdate
. It needs some code movement to make it happen. Like @DonJayamanne suggested, probably we leave it to him for the refactoring.