DS-4799 Slice refactoring

Adding parameters to pass to pydev

GitOrigin-RevId: 0ee5427de4b48bae002d1b4ffc07821eccfe8499
This commit is contained in:
Anton Bragin
2023-04-26 12:16:56 +02:00
committed by intellij-monorepo-bot
parent 423926bde7
commit b4152bd2df
6 changed files with 65 additions and 39 deletions

View File

@@ -5,8 +5,6 @@ from _pydevd_bundle import pydevd_vars
from _pydevd_bundle.pydevd_constants import NEXT_VALUE_SEPARATOR
from _pydevd_bundle.pydevd_xml import ExceptionOnEvaluate
import sys
MAX_COLS = 500
MAX_COLWIDTH = 200
@@ -27,43 +25,29 @@ def is_error_on_eval(val):
def exec_table_command(init_command, command_type, f_globals, f_locals):
# noinspection PyUnresolvedReferences
res = ""
table = pydevd_vars.eval_in_context(init_command, f_globals, f_locals)
is_exception_on_eval = is_error_on_eval(table)
if is_exception_on_eval:
return False, table.result
table_provider = __get_table_provider(table)
if not table_provider:
raise RuntimeError('No table data provider for: {}'.format(type(table)))
res = []
if command_type == TableCommandType.DF_INFO:
if 'pd' not in sys.modules:
exec('import pandas as pd', f_globals, f_locals)
tmp_var = pydevd_vars.eval_in_context(init_command, f_globals, f_locals)
is_exception_on_eval = is_error_on_eval(tmp_var)
if is_exception_on_eval:
return False, tmp_var.result
table_provider = __get_table_provider(tmp_var)
if not table_provider:
raise RuntimeError('No table data provider for: {}'.format(type(tmp_var)))
res += table_provider.get_type(tmp_var)
res += NEXT_VALUE_SEPARATOR
res += table_provider.get_shape(tmp_var)
res += NEXT_VALUE_SEPARATOR
res += table_provider.get_head(tmp_var, MAX_COLS)
res += NEXT_VALUE_SEPARATOR
res += table_provider.get_column_types(tmp_var)
res.append(table_provider.get_type(table))
res.append(NEXT_VALUE_SEPARATOR)
res.append(table_provider.get_shape(table))
res.append(NEXT_VALUE_SEPARATOR)
res.append(table_provider.get_head(table, MAX_COLS))
res.append(NEXT_VALUE_SEPARATOR)
res.append(table_provider.get_column_types(table))
elif command_type == TableCommandType.SLICE:
import pandas as pd
_jb_max_cols = pd.get_option('display.max_columns')
_jb_max_colwidth = pd.get_option('display.max_colwidth')
pd.set_option('display.max_colwidth', MAX_COLWIDTH)
tmp_var = pydevd_vars.eval_in_context(init_command, f_globals, f_locals)
is_exception_on_eval = is_error_on_eval(tmp_var)
if is_exception_on_eval:
return False, tmp_var.result
res += repr(tmp_var.to_html(notebook=True,
max_cols=MAX_COLS))
pd.set_option('display.max_colwidth', _jb_max_colwidth)
res.append(table_provider.get_data(table, MAX_COLS))
return True, res
return True, ''.join(res)
def __get_table_provider(output: str):

View File

@@ -1,5 +1,7 @@
# Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import io
import sys
import pandas as pd
def get_type(table) -> str:
@@ -19,3 +21,37 @@ def get_column_types(table) -> str:
print(table.index.dtype, *[str(t) for t in table.dtypes],
file=output)
return output.getvalue()
# TODO: check if we actually need all three
# used by pydevd
def get_data(table, max_cols):
_jb_max_cols = pd.get_option('display.max_columns')
_jb_max_colwidth = pd.get_option('display.max_colwidth')
pd.set_option('display.max_colwidth', max_cols)
data = repr(table.to_html(notebook=True, max_cols=max_cols))
pd.set_option('display.max_colwidth', _jb_max_colwidth)
return data
# used by DSTableCommands
def get_data_slice(table, start, end):
return table.iloc[start:end]
# used by DSTableCommands
def display_data(table, max_cols, max_colwidth, start, end):
from IPython.display import display
_jb_max_cols = pd.get_option('display.max_columns')
_jb_max_colwidth = pd.get_option('display.max_colwidth')
pd.set_option('display.max_columns', max_cols)
pd.set_option('display.max_colwidth', max_colwidth)
display(table.iloc[start:end])
pd.set_option('display.max_columns', _jb_max_cols)
pd.set_option('display.max_colwidth', _jb_max_colwidth)

View File

@@ -13,6 +13,7 @@ import com.jetbrains.python.debugger.pydev.PyDebugCallback;
import com.jetbrains.python.debugger.pydev.TableCommandType;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandBuilder;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandResult;
import com.jetbrains.python.debugger.pydev.tables.TableCommandParameters;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -98,7 +99,7 @@ public interface PyFrameAccessor {
}
@Nullable
String execTableCommand(String command, TableCommandType commandType) throws PyDebuggerException;
String execTableCommand(String command, TableCommandType commandType, TableCommandParameters tableCommandParameters) throws PyDebuggerException;
@Nullable
default XCompositeNode getCurrentRootNode() {

View File

@@ -3,4 +3,7 @@ package com.jetbrains.python.debugger.pydev.tables
enum class CommandOutputType {
STREAM, DISPLAY
}
}
interface TableCommandParameters {}

View File

@@ -32,6 +32,7 @@ import com.jetbrains.python.debugger.pydev.SetUserTypeRenderersCommand;
import com.jetbrains.python.debugger.pydev.TableCommandType;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandBuilder;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandResult;
import com.jetbrains.python.debugger.pydev.tables.TableCommandParameters;
import com.jetbrains.python.debugger.settings.PyDebuggerSettings;
import com.jetbrains.python.debugger.variablesview.usertyperenderers.ConfigureTypeRenderersHyperLink;
import com.jetbrains.python.debugger.variablesview.usertyperenderers.PyUserNodeRenderer;
@@ -341,7 +342,7 @@ public abstract class PydevConsoleCommunication extends AbstractConsoleCommunica
}
@Override
public String execTableCommand(String command, TableCommandType commandType) throws PyDebuggerException {
public String execTableCommand(String command, TableCommandType commandType, TableCommandParameters tableCommandParameters) throws PyDebuggerException {
if (!isCommunicationClosed()) {
return executeBackgroundTask(
() -> {

View File

@@ -63,6 +63,7 @@ import com.jetbrains.python.debugger.containerview.PyViewNumericContainerAction;
import com.jetbrains.python.debugger.pydev.*;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandBuilder;
import com.jetbrains.python.debugger.pydev.dataviewer.DataViewerCommandResult;
import com.jetbrains.python.debugger.pydev.tables.TableCommandParameters;
import com.jetbrains.python.debugger.settings.PyDebuggerSettings;
import com.jetbrains.python.debugger.smartstepinto.PySmartStepIntoContext;
import com.jetbrains.python.debugger.smartstepinto.PySmartStepIntoHandler;
@@ -796,7 +797,7 @@ public class PyDebugProcess extends XDebugProcess implements IPyDebugProcess, Pr
}
@Override
public String execTableCommand(String command, TableCommandType commandType) throws PyDebuggerException {
public String execTableCommand(String command, TableCommandType commandType, TableCommandParameters tableCommandParameters) throws PyDebuggerException {
final PyStackFrame frame = currentFrame();
return myDebugger.execTableCommand(frame.getThreadId(), frame.getFrameId(), command, commandType);
}