Blog,
Using IPython within VS Code
I like using VS Code for software development. I really like:
- the huge number of extensions
- very good customization options
- quick start
- very good Python extensions
Coming from Spyder and scientific applications, I use a Python distribution called Winpython and also appreciate IPython. Switching to VS Code was trickier than thought. To use the Winpython distribution in VS Code’s Python extensions an entry to the settings.js file was necessary:
"python.defaultInterpreterPath":
"C:\\portable\\WPy64-3720\\python-3.7.2.amd64",
But I was then missing the IPython console. Another extension IPython for VS Code, enables starting a script in a IPython console which will be started in the integrated terminal. To use this extension with Winpython I had to modify the source code of the extension.
The extension Power Tools enables to add buttons and commands to VS Code without writing an extension.
So I added to buttons Sent To IPython and IPython:
This is done with the following entry in the settings.js file:
"ego.power-tools": {
"buttons": [
{
"text": "IPython",
"tooltip": "Create shell and start IPython",
"action": {
"type": "script",
"script": "ipython.js"
}
}
],
"commands": {
"sendToIPython": {
"script": "sendToIPython.js",
"button": {
"text": "Sent To IPython"
}
}
},
"user": {
}
},
The two script files referenced above are located in my user directory in the .vscode-powertools subdirectory: They look like this:
ipython.js (Replace hard coded path as needed)
exports.execute = async (args) => {
const vscode = args.require('vscode');
var ipython_shell = get_ipython_shell(vscode);
if (ipython_shell==null)
{
ipython_shell = vscode.window.createTerminal("IPython")
setTimeout(() => {
get_ipython_shell(vscode).sendText("C:\\portable\\WPy64-3720\\scripts\\cmd.bat")
setTimeout(() => {
get_ipython_shell(vscode).sendText("ipython")
}, 100);
}, 1000);
}
ipython_shell.show(false)
};
function get_ipython_shell(vscode)
{
var ipython_shell = null;
vscode.window.terminals.forEach(element => {
if (element.name=="IPython")
{
ipython_shell = element
}
});
return ipython_shell
}
sendToIPython:
exports.execute = async (args) => {
const vscode = args.require('vscode');
var ipython_shell = get_ipython_shell(vscode);
const editor = vscode.window.activeTextEditor;
if (editor == undefined)
{
vscode.window.showInformationMessage(
"No active Editor"
);
return
}
// save current file
let success = await vscode.commands.executeCommand('workbench.action.files.save');
const filename = editor.document.fileName;
var command_zw = null
var shebang = false
if (editor.document.lineAt(0).text[0]=="#")
{
shebang = 1
}
let startLine, endLine;
if (editor.selection.isEmpty) {
command_zw = `%run ${filename}`;
} else {
if (shebang==1)
{
startLine = editor.selection.start.line;
endLine = editor.selection.end.line;
}
else
{
startLine = editor.selection.start.line + 1;
endLine = editor.selection.end.line + 1;
}
command_zw = `%load -r ${startLine}-${endLine} ${filename}`;
}
var delay = 0;
const command = command_zw
if (ipython_shell == null)
{
delay = 1300
ipython_shell = vscode.window.createTerminal("IPython")
setTimeout(() => {
get_ipython_shell(vscode).sendText("C:\\portable\\WPy64-3720\\scripts\\cmd.bat")
setTimeout(() => {
get_ipython_shell(vscode).sendText("ipython")
}, 100);
}, 1000);
}
setTimeout(() => {
ipython_shell.show();
get_ipython_shell(vscode).sendText(command)
setTimeout(() => {
get_ipython_shell(vscode).sendText("")
}, 100);
}, delay);
};
function get_ipython_shell(vscode)
{
var ipython_shell = null;
vscode.window.terminals.forEach(element => {
if (element.name=="IPython")
{
ipython_shell = element
}
});
return ipython_shell
}