Plugin Development: Fetch variable value (numpy array)

Answered

Hello.

 

I'd like my plugin has access to values of variables while stopped in debugger.

Currently i got some code:

public void actionPerformed(AnActionEvent event) {
Project project = event.getProject();
//Messages.showMessageDialog(project, "Hello world!", "Greeting", Messages.getInformationIcon());
XValueNodeImpl node = XDebuggerTreeActionBase.getSelectedNode(event.getDataContext());
XValue container = node.getValueContainer();
}

In this simple code in container variable I have type of my debugging variable. I can check that it has ndarray type, for example. But I don't know how to obtain values in this array.

The second question. Is there any chance to get that variable in "python style". In other words, can I continue working with this variable (as a part of my plugin) not in Java, but in Python. My plugin is supposed to be developed only for PyCharm and python code. So it would be convenient for me to keep working in python. E.g. I'd like to plot this array using matplotlib. Is it possible?

Thanks

1
1 comment

Hello @Jorikdima,

You can get the contents of an array using the code snippet below:

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.xdebugger.impl.ui.tree.actions.XDebuggerTreeActionBase;
import com.jetbrains.python.debugger.ArrayChunk;
import com.jetbrains.python.debugger.PyDebugValue;
import com.jetbrains.python.debugger.PyDebuggerException;
import com.jetbrains.python.debugger.PyFrameAccessor;
import org.jetbrains.annotations.NotNull;

public class PyFetchValue extends AnAction {

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
PyDebugValue value = (PyDebugValue)XDebuggerTreeActionBase.getSelectedValue(e.getDataContext());
if (value == null) return;
PyFrameAccessor frameAccessor = value.getFrameAccessor();
try {
ArrayChunk arrayChunk = frameAccessor.getArrayItems(value, 0, 0, -1, -1,"%d");
Object[] arrayData = arrayChunk.getData();
// do something with `arrayData`
}
catch (PyDebuggerException ex) {
// handle the exception
}
}
}

I would also like to encourage you to take a look at the PyDataView class implementation which does a very similar thing of what you're doing.

Now to your second question. I'm not aware of any IDEA public API that gives you this functionality. This means that you either will need to process data on the Java side or implement some helper Python service that will be able to receive arrays, produce plots, and send them back to the IDE.

Hope this will help. Don't hesitate to ask you have any other questions.  

0

Please sign in to leave a comment.