Running IPython Notebook within Sublime Text 2 - plugins

I'm a ST2 user (mainly on Python) and I use IPython notebook as well.
Before, it was all fine, I could run IPython notebook from ST2 command (ST2 then opened a new tab and there are interactive cells etc.)
Since yesterday I upgraded ipython to version 2.0 using pip. From then on I cannot have the notebook tab in ST2 anymore. Trying to input the command 'open ipython notebook' to ST commandline returns nothing:
>>> "open ipython notebook"
'open ipython notebook'
Is this the problem of compatibility? Maybe the plug in of ST2 does not work with version of ipython anymore? I notice that in version 2.0, the link to the home page of the ipython server changed a bit. Can you suggest how to manually edit the configuration of ST2 ipythonnotebook plugin to make it run again?
>>> open ipython notebook
File "<string>", line 1
open ipython notebook
^
SyntaxError: invalid syntax

There are a couple of issues. First, IPython 2.0 doesn't work with the IPython Notebook plugin. This was reported 3 months ago, and the developer hasn't done anything about it yet.
Second, even if you hadn't upgraded IPython, you're entering open ipython notebook in the wrong location. You should be typing that into the Command Palette (CtrlShiftP on Windows/Linux, ⌘⇧P on OS X), not the Sublime console (Ctrl`).
So, there are two options. The first is to downgrade your version of IPython back to the one you were using before, and everything will work again. The downside of this is you won't have access to the latest features of IPython, which may or may not be an issue depending on your workflow.
The second option is to run IPython via the SublimeREPL plugin. One nice thing about this is the ability to see images as you generate them, as they pop up in new windows. Downsides include the current lack of ability to connect to running notebook servers, the fact that you can't save your work as an .ipynb file, and the different way of presenting data - it's more line-based, not cell-based, so for example if I define a loop:
In [1]: for i in range(10):
...: print(i)
and want to modify and rerun it later, using the arrow keys to go back through the history only goes line-by-line, not cell-by-cell. That means you'd have to rewrite your loop each time you want to run it. I mainly use SublimeREPL/IPython to test snippets of code as I'm writing to make sure they do what I want them to.

Instead of running a notebook, you can directly call ipython using following configuration at the keymap bindings (Preferences > Key Bindings - User):
[{ "keys": ["f9"], "command": "repl_open",
"caption": "Python - IPython",
"id": "repl_python_ipython",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": {
"osx": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"linux": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"windows": ["python", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
},
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"
}
}
}
]
So, By clicking F9, it will open ipython on new shell

Related

VS Code- "Run" button not appearing for my Jupyter Notebook, even after installing extensions and setting kernel

TL;DR: how can I run my Jupyter notbook in VS Code, given I've tried the previously-suggested solutions (like installing certain extensions) and nothing appears to be working?
I've got the following blank .ipynb file:
I've installed both Microsoft's Jupyter and Python extensions installed:
I created the file in screenshot #1 above via the command palette to Jupyter > Create New Blank Notebook. We see a cell which I've changed from plain text to Python. I don't see a "Run" button in my editor like I do in the VS Code + Jupyter docs. I hit Shift-Enter to run the cell, but instead of the expected output, I'm prompted to "Select a kernel for Untitled-1.ipynb`:
I try to type in my best guess for which kernel they're referring to (I use Python 3.9.5 in my terminal), but typing things like 3.9.5 and Python, thinking I might see an auto-complete helper or a dropdown with valid options to choose from, but this didn't happen.
The "Restart" and "Interrupt" buttons are greyed-out and disabled, implying that the Jupyter server isn't running. However, the docs imply that I should be able to just create a new Jupyter notebook and start running code immediately:
Here’s how to get started with Jupyter in VS Code.
If you don’t already have an existing Jupyter Notebook file, open the
VS Code Command Palette with the shortcut CTRL + SHIFT + P (Windows)
or Command + SHIFT + P (macOS), and run the “Python: Create Blank New
Jupyter Notebook” command.
If you already have a Jupyter Notebook
file, it’s as simple as just opening that file in VS Code. It will
automatically open with the new native Jupyter editor.
Once you have a Jupyter Notebook open, you can add new cells, write code in cells, run cells, and perform other notebook actions.
I saw the following Jupyter Server: local component at the bottom of my screen. I see the icon on the left-hand side shows a disconnected power cord; not sure if that implies the server itself is disconnected. At any rate, I tried clicking on this component to see what would happen:
I see the default behavior is already selected, which leads me to believe that VS Code would automatically start up a Jupyter server when the app itself is launched.
I also tried setting the kernel via the command palette (Jupyter > Select interpreter to start Jupyter server), and this time I did see my version of Python. However, selecting that option didn't seem to resolve the issue, since I still don't see a "Run" button:
I tried looking in all the tabs at the bottom of the screen (Terminal, Jupyter Variables, Debug Console, Problems, and Output), but didn't see anything at all.
Interestingly, when I run Jupyter > Create Interactive Window from the command palette, I am able to create Jupyter cells and run Python code in them. However, I consider this to be a workaround and not a solution to my primary problem of being unable to run both interactive sessions and notebooks:
These are all my VS Code settings:
{
"ruby.intellisense": "rubyLocate",
"files.autoSave": "onFocusChange",
"workbench.editor.showTabs": true,
"diffEditor.ignoreTrimWhitespace": false,
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2,
"editor.tabCompletion": "on",
"workbench.colorCustomizations": {
"editorUnnecessaryCode.border": "#dd7aab"
},
"window.zoomLevel": 1,
"terminal.integrated.shell.osx": "/bin/zsh",
"[yaml]": {
"breadcrumbs.showEvents": true,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.autoIndent": "advanced"
},
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"workbench.editor.untitled.hint": "hidden",
"workbench.editorAssociations": {
"*.ipynb": "jupyter-notebook"
},
"notebook.cellToolbarLocation": {
"default": "right",
"jupyter-notebook": "left"
},
"python.defaultInterpreterPath": "/usr/local/bin/python3",
"notebook.lineNumbers": "on",
"jupyter.variableQueries": [
],
}
What am I missing?
Here is your answer,
1.Just refresh your IDE and then restart your IDE .
2.INSTALL KERNEL ON YOUR IDE.
Sometimes extensions in IDE must not have hosted therefore the extensions does not work.In this case just restart your IDE and then host your extensions.
I am not sure if you're still searching for an answer.
What worked for me was to install conda (anaconda or miniconda will do).
Then in the command prompt, type:
conda install ipykernel
Restart your IDE and it should work!
I had this issue too and solved it by:
pip install ipython
After installing this package and Jupyter extensions in Visual Studio code, the run button is available left of the cell and you can click it to run the cell.
Then it tries to connect to the kernel and for the first time asks you to install Ipykernel if it doesn't show this message, etc you can run this code in Command Prompt:
python -m pip install ipykernel
also for more information, you can see this page:
https://ipython.readthedocs.io/en/latest/install/index.html
Go to Extension
Search "Jupyter" from Microsoft
Click "Switch to Pre-Release Version"
Click "Reload"
VS Code Extension

VS Code: jupyter notebook run line by line

Recently VSCode got an update and after that my jupyter notebook interface has changed which created a lot of problems for me. Now I don't know how to run cell code line by line, etc.
Is there any other way to do so or how can I get back to an older version of the notebook?
Open the command palette (Ctrl+Shift+P on Windows, Cmd+Shift+P on Mac)
Type 'Preferences: Open Settings (JSON)' and select the first option to open your user settings.json file
Add the following line to your user settings.json file:
"jupyter.experiments.optOutFrom": ["NativeNotebookEditor"],
If the following entry is present in your user settings.json file, delete it:
"workbench.editorAssociations": [
{
"viewType": "jupyter-notebook",
"filenamePattern": "*.ipynb"
}
]
Reload Visual Studio Code for the new settings to take effect. You should be opted back into the old Jupyter notebook interface.
If you want to run a python script line by line using Jupyter interpreter this is the option you need to tick in VS code settings.
In v1.60 (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_60.md#jupyter):
VS Code now supports the Run By Line feature in Jupyter notebooks.
This is a simple debug mode that lets you execute the code of a cell
line by line. To try it out, just make sure that you have ipykernel v6+ installed as your selected kernel, then select the Run By Line
button.
Previously:
In vscode 1.59 (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_59.md#jupyter-run-by-line)
Jupyter "Run By Line"
We've been working on supporting the "Run By Line" feature in Jupyter
notebooks. This feature is essentially a simplified debug mode that
lets you step through your cell's code line by line without any
complex debug UI. This is still experimental, but you can try it out
by setting "jupyter.experimental.debugging": true, installing
version 6 of ipykernel in your selected kernel, then clicking the "Run
By Line" button in the cell toolbar.
"jupyter.experimental.debugging": true

How to run scheme program in VS Code

Just started to learn SICP (Structure and Interpretation of Computer Program). I installed mit-scheme compiler. I am able to run the interpreter by just typing scheme in terminal. I also have VSCode installed along with linting support for scheme.
Now I need to write, compile and launch the scheme program from VSCode.How do I do that. I dont need to debug line by line. I am new to VS code.
PS: I can write program, edit it and check for lint errors. I am unable to launch the scheme compiler out of the box. I am missing some steps in editing few jsons to get this done.
PS PS: I prefer VSCode and I want mit-scheme to interpret/ compile my program. Do not want to use racket or code runner extension. Thanks!
Main menu->Terminal->Configure tasks
Create new task:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run scheme",
"type": "shell",
"command": "scheme",
"args": ["<", "${file}"]
}
]
}
Open file with scheme code
Main menu->Terminal->Run Task->run scheme
on mac os with chezscheme
install chezscheme
brew install chezscheme
install code runner extension of vscode
setup code runner in setting.json
"code-runner.executorMap": {
"scheme": "chez --script"
},
run scheme
create hello.ss
; Hello World
(display "Hello World")
(exit)
click run code button in top right or menu.
Most Scheme systems do not compile. They are interpreters. That is the reason why the book is called Structure and Interpretation of Computer Programs. The typical interaction with an interpreter is the REPL, the Read Eval Print Loop. The easiest way to write Scheme code is to use an editor like Emacs and execute run-scheme. After that you can send any s-expression with Ctrl-x Ctrl-e from the editor to the interpreter. Emacs shows the result in the REPL window.
What a coincidence! I'm learning SICP too.
I'm on a Mac. My way of running scheme programs in VS code is control+shift+ to open its terminal and the input scheme > foo.sch. (replace foo.sch with you file name).
And you can do this in the built-in Terminal app too, without VS Code.

Activate conda environment stopped working in vscode

I have noticed that conda activate environment stopped working in vscode integrated terminal with no obvious reason (I certainly did not install any new extensions, nor updated conda or vscode in the last few days).
Expected behaviour
After running conda activate environment I want the environment to be activated and be capable of using the environment python interpreter. Essentially it should look like this:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/Users/lpravda/anaconda3/envs/rdkit-env/bin/python
This is what I can achieve using non-vscode terminal and work with conda as usual.
Actual behaviour
This happens when I type in the same set of commands in vscode integrated terminal:
(base) lpravda-ml:~ lpravda$ conda activate rdkit-env
(rdkit-env) lpravda-ml:~ lpravda$ which python
/usr/local/bin/python
The environment seems to be switched on, however python still points to the system install python 2.7 and I have no idea why.
It used to be working like for years and now it broke down.
Note: When I run debugger in vscode the correct conda environment is selected and executed.
My setup:
OS: Mac OS X: 10.14.3
vscode: 1.31.1
conda: 4.6.7
Update
ongoing issue with the following update:
OS: Mac OS X: 10.14.3
vscode: 1.33.3
conda: 4.6.8
Any idea what could possibly broke down?
it is issue which you can read about it here, but I find this solution
which solve the issue in this version. Also, as community said the problem with python interpreter and env gonna fix in next major update.
Create an workspace folder
Inside the create file post the following code:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.condaPath": "/home/pcuser/anaconda3/condabin/conda",
"python.pythonPath": "/home/pcuser/anaconda3/envs/my_env/bin/python",
"python.terminal.activateEnvironment": true,
"python.terminal.activateEnvInCurrentTerminal": true,
}
}
Save configuration and close vscode
Open vscode.
It will select automatically the conda enviroment (Should appear at the bottom left of vscode). Inside terminal 'my_env' will also be selected. If vscode doesn't select the 'Python Selected Interpreter' before you open the terminal the my_env inside terminal won't be selected. Open the terminal after the my_env is selected by vscode

How to add multiple terminals in VS Code?

Can we add multiple different terminals in the VS Code? I am planning to add following three terminal and work with all of those :
Windows Command prompt
PowerShell
Git Bash
I know I need to add the following command in Preferences => Setting
// // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
I want to add all of the above three commands in setting.json
And when I click + different terminal should open and I want to work with those terminals without changing the preferences.
Is it possible in VS Code or not?
There is a way to make this happens with these steps by installing an extension:
Find an extension called Shell launcher and install it or you can find it here. Reload VS Code if you want or after you finished all steps.
Go to Files --> Preferences --> Settings and this will open settings.json file and you then insert this (you can edit this to your heart's content):
Code:
"shellLauncher.shells.windows": [
{
"shell": "C:\\Windows\\<sysnative>\\cmd.exe",
"label": "cmd"
},
{
"shell": "C:\\Windows\\<sysnative>\\WindowsPowerShell\\v1.0\\powershell.exe",
"label": "PowerShell"
},
{
"shell": "C:\\Program Files\\Git\\bin\\bash.exe",
"label": "Git bash"
},
{
"shell": "C:\\Windows\\<sysnative>\\bash.exe",
"label": "WSL Bash"
}
]
PS: You can use shellLauncher.shells.linux for Linux or shellLauncher.shells.osx for macOS.
Go to Files --> Preferences --> Keyboard Shortcuts and then find on {} icon on the top right corner to open keybindings.json file. Insert this:
Code:
[
{ "key": "ctrl+alt+`", "command": "shellLauncher.launch" }
]
Update: Type shelllauncher into the search bar. You can then see Shell Launcher: Launch command. Highlight and use any keybinding you like. For example, I picked Ctrl + Alt + (backtick)` for myself.
You can reload your VS Code and click the key combination you have assigned and that will give you the option to choose which integrated terminal you want to use.
For more details, please check the official site: https://marketplace.visualstudio.com/items?itemName=Tyriar.shell-launcher
Enjoy!
Even the question is asked last year and the answer is accepted but still I feel to answer this question as I didn't found any simple, suitable and complete answer while as a development I need multiple terminal handy in a click like below:-
and I don't bother about their path, add another extension for what the VS Code is already capable of or reload VS Shell etc and go to insert and setup the settings files manually.
I found this question is asked many times and almost all landed up manually setup of write some settings etc. or sometimes only opted to get a single type of terminal. The answer of #Pawan is somewhat near but again that solution finally land up to a single terminal, going to command setup for switch terminal and this one will work for git or any other terminal.
If you have tools installed which worked on command line like power-shell and git along with default cmd prompt in windows then the follow the quick three steps to get all terminals at once and switch to anyone with a click.
Open terminal, it should be visible (use ctrl+` or from menu View-> Integrated Terminal )
Open commands search (use Ctrl+Shift+P or from menu View->Command Palette...)
In command box Type "Terminal: Select Default Shell" to select this option from drop down.
As you select this option, then all the available commands which are in path will be listed below like below
Just click any one which you like to add for quick access from command list.
Finally, in the terminal window, just click on + sign next to terminal list as shown below:-
The terminal selected in step 5 will now added after performing step6 to the list without removal of earlier terminal.
Repeat step 3-6 for adding any other terminal to command list.
To work with particular terminal just select the required one in the terminal list of the terminal window.
press ctrl + shift + ` shortcut, or press a cross sign to run new terminal, then type bash if your default mode is powershell or powershell if your default mode is bash. And here you are, your terminal is switched.
For now VS Code support defining only one of available terminals as default at a time and you can not add multiple shell terminals.
If you don't want to install "Shell Launcher" plugin as suggested by #ian0411 then here is a quick way to change/select default terminal.
Press "Ctrl + Shift + P" to open the Command Palette.
Type "Terminal: Select Default Shell" and hit enter. You will be prompted to select Git Bash, PowerShell or Cmd as shown below:
Note: This will change the default shell terminal but there is no harm changing it whenever you need to use another.
BTW, if you need to switch only between Cmd & Powershell then you can type cmd or powershell (in already opened terminal) to switch to desired terminal. But it would not work for Git Bash.
I don't see this in the above, but read all the answers. I think this is the best approach for what is supported right now. I believe, like myself, the OP simply wants to open VSCode, and down yonder on the terminal window, we just want a list of options to open from.
Yes, you can open the command, "Terminal: Select Default Shell" and loop through that to add various types of terminals...
Followed by:
I don't know when this option was added to the dropdown, but look! No need to start by looking up the command. I think this is a lot smoother than any other answer, but it still results in the annoying overwrite of your default term to whatever your last choice was before leaving session.
Here, we can more easily open the Select Default Shell - it should support "Select New Shell". Simple, you'd think. Hopefully someone adds that soon or I have time to contribute.
I'm fairly certain that these are old-style settings.json and won't work as discussed in VS Code 2021. The new style looks like:
...
"terminal.integrated.profiles.windows":
{ "Bash":
{ "path": ["C:\\Programs\\Git\\bin\\bash.exe"],
"icon": "terminal-bash",
},
"Command Prompt":
{ "path": ["${env:windir}\\System32\\cmd.exe"],
"args": [],
"icon": "terminal-cmd"
},
"PowerShell":
{ "path": ["C:\\Programs\\PowerShell\\pwsh.exe"],
"source": "PowerShell",
"args": [],
"icon": "terminal-powershell"
},
},
"terminal.integrated.defaultProfile.windows": "Bash"
...
That will get you access to Command and PowerShells and set bash as the default shell (my preference). If you want to launch a standalone application like git-bash, you will need something else.
As of https://code.visualstudio.com/updates/v1_35 you can now:
select from the pulldown menu directly above the terminal "Select Default Shell"
select the one you like
click the +
done
(the same goes for opening split screen: before this do 1 + 2 and then click the split screen button)
(although overriding the default but does no longer matter if this is your flow)
This can be done by adding a different key at the end. By just changing your example to:
// // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows2": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows3": "C:\\Program Files\\Git\\bin\\bash.exe",
Note that the key ...shell.windows is changed to ...shell.windows2 and ...shell.windows3.
Follow-up finding: I've noticed that once restart of the IDE, only the first terminal gets displayed. I had to reopen the settings and save it once to get both the terminals again. Will post if any better solution available.
In the terminal tab, there's a Split Terminal button. Works like a charm
To open the multiple terminal please check the screen shot for the same(on the right bottom of the visual studio code their will be a dropdown and just after it, their is +(plus) icon . On pressing it the new terminal will open.).
The recommended way to automatically open multiple terminal windows is to use the Tasks feature. See Automating launching of terminals in the VS Code documentation.
For WSL Ubuntu on Windows terminal:
File -> preferences -> settings -> click code icon in top right
Enter the following:
{
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
"git.enableSmartCommit": true
}