In VScode I can write a python file with markdown and python cells and then convert it to a notebook via the command palette. Everything works well, but I would like to automate this with a task.
I know I could just define a shortcut for the conversion but then I would still need to manually save the notebook with the file explorer. Can I automate this with a task? If so how do I access the function for the conversion? Is this some internal function of VScode or can I access this function via the command line?
I tried a few things with the jupyter command in the command line but didn't have any luck. It seems like there is no command to convert a python file to a Jupyter notebook. Also I could not find a comprehensive documentation for the jupyter command.
Another question in regards to Jupyter notebooks in VScode: Is there a way how I can hide a cell from showing up? I know it would be possible if I edit the metadata of the notebook, but I hope there is a better way.
In case anyone else is still wondering about this, I'm now using jupytext for converting python files to jupyter notebooks.
I wrote two very simple bash scripts to automate the conversion and viewing of notebooks.
conversion:
#!/usr/bin/env bash
# retrieve file names and folder from arguments
full_fname=$1
fbase_name_no_ext=$2
dir_name=$3
workspace_folder=$4
full_path_no_ext="$dir_name/$fbase_name_no_ext"
notebook_save_path=$(cd ${dir_name}/.. && pwd)
# activate venv (venv should be in vscode workspace root)
source "${workspace_folder}/my_env/bin/activate"
echo "saving to: $notebook_save_path"
# convert to jupyter notebook
jupytext --to notebook ${full_fname}
# run all cells and save output to notebook file
jupyter nbconvert --to notebook --execute "${full_path_no_ext}.ipynb" --output "${notebook_save_path}/${fbase_name_no_ext}"
# cleanup intermediate notebook (contains only cells no output)
rm "${full_path_no_ext}.ipynb
viewing a notebook:
#!/usr/bin/env bash
# retrieve file names and folder from arguments
fbase_name_no_ext=$1
dir_name=$2
workspace_folder=$3
source "${workspace_folder}/my_env/bin/activate"
# notebooks are stored in the parent directory of the source file
notebook_folder=$(cd ${dir_name}/.. && pwd)
# view notebook in a browser window
jupyter notebook "${notebook_folder}/${fbase_name_no_ext}.ipynb"
Here is my vscode tasks file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "convert to NB",
"type": "shell",
"command": "${workspaceFolder}/convert",
"args": [
"${file}",
"${fileBasenameNoExtension}",
"${fileDirname}",
"${workspaceFolder}"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{
"label": "view NB",
"type": "shell",
"command": "${workspaceFolder}/viewNB",
"args": [
"${fileBasenameNoExtension}",
"${fileDirname}",
"${workspaceFolder}"
]
}
]
Since I'm no bash expert, there might be multiple things that could be done in a better way. Critique is always welcome!
Hope this is helpful to someone.
Related
I am trying to debug an azure function app in VSCode using Python in a Windows10 environment. Whenever I launch the debugger it hangs for a long period of time and then opens a message box that says
ECONNREFUSED 127.0.0.1:9091
There are a bunch of posts about this but none seem helpful/can solve the problem. Here is what I've tried:
uninstalling and re-installing different versions of
azure-function-core-tools using windows installer, npm and chocolatey
uninstalling and re-installing Azure Functions extension in VS Code
changing the extension bundle
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
}
modifying local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}
deleting C:\Users\admin.azure-functions-core-tools\Functions\ExtensionBundles
creating a function app from command line using "func init" and lauching debugger by running "func host start" in active venv
I am using Python38 and really have no idea what else to try. Any suggestions are welcome.
Thanks!
Cannot launch debugger for azure function app in VScode-
ECONNREFUSED 127.0.0.1:9091
This type of generic error may occur for a variety of reasons.
Need to check and modify:
First and foremost, check whether the versions of Azure functions core tools and Pip are upgraded to the current version:
To upgrade pip:
python -m pip install --upgrade pip
To install and upgrade azure-functions:
pip install azure-functions
Go to the below path,
view -> Command palette -> User Settings
Python functions, task runFunctionsHost windows command only work with powershell:
Set the integrated > default profile: Windows to PowerShell as PowerShell runtime host is functional with Python functions. It was previously set to "null".
The debug configuration is specified in your tasks.json and launch.json files in the .vscode folder.
As stated here , the default listening port in launch.json is set to 9091. Here, I updated it to "port: 7071" which is open for traffic on my function project, launched the files, and then executed the "Attach to Python Functions" debug task once again.
Under .VScode folder -> launch.json file, this configuration changes works for me.
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "python_modules./.bin/func",
"console": "integratedTerminal"
},
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 7071,
"preLaunchTask": "func: host start"
}
]
}
Added multiple debug points, debugged and triggered successfully as shown below:
Also Check here for more approaches given by #Hari Krishna
found the solution at:
https://github.com/Azure/azure-functions-core-tools/issues/3160#issuecomment-1266273749
I ran command func start in verbose mode
func start --verbose
from there it was clear that the process timed out when trying to download a new extension bundle. Most likely due to slow internet. I manually installed the new extension bundle:
https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/3.15.0/Microsoft.Azure.Functions.ExtensionBundle.3.15.0_any-any.zip
(the full path should be in the --verbose output) and extracted to
C:\Users[user name].azure-functions-core-tools\Functions\ExtensionBundles\Microsoft.Azure.Functions.ExtensionBundle\3.15.0
It now works. Thanks everyone for input.
With my conda environment activated I set an environment variable with the command
conda env config vars set ROOT_DIRECTORY=$PWD
Now, if a run echo $ROOT_DIRECTORY the output shows /home/augusto/myproject
How can I get that variable inside Jupyter Notebook? I tried with the command below, but the output shows None.
import os
print(os.getenv('ROOT_DIRECTORY'))
By the way, I have shure that Jupyter Notebook are using the correct Kernel. Running the above code inside a .py file works correctly, i.e. the output shows /home/augusto/myproject.
Find env dir
jupyter kernelspec list
cd path_to_kernel_dir
sudo nano kernel.json
Add environment section to json
json should look something like this
{
"argv": [
"/home/jupyter-admin/.conda/envs/tf/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"env": {"LD_LIBRARY_PATH":"/home/jupyter-admin/.conda/envs/tf/lib/"},
"display_name": "tf_gpu",
"language": "python",
"metadata": {
"debugger": true
}
}
When I run tests with the VS Code pytest extension, it prints the results to the integrated output, but I'd rather have them in in the integrated terminal.
I found this, but unfortunately the settings code snippet which might exposes what I am looking for, has dead links on the images and does not show up (couldn't find it in the corresponding github repo neither) :(
Does anyone has a clue how to change this (probably in settings.json)? Is it even possible to print the results in the integrated terminal?
Thanks in advance!
There isn't a way to have test runs triggered via the extension print out to the terminal. You will need to run your tests manually in the terminal to get the output there.
at settings.json add
"python.testing.pytestArgs": [
"-s"
],
if already there is an arg, just add
,"-s"
Try again with the latest update (on 1.61.0 as of writing this) — at this point in time, I am getting the output in the integrated terminal, too.
I tried to add -s to pytest.ini or setting.json and none of them worked. The only way I was able to make something decent was by creating a vscode task:
Just create a simple json Task and use this simple script to either run all the tests in a file or select the function name and run the other task to run a single pytest:
{
"version": "2.0.0",
"tasks": [
{
"label": "pytest-file",
"type": "shell",
"command": "pytest ${file} -s"
},
{
"label": "pytest-test",
"type": "shell",
"command": "pytest ${file} -k ${selectedText} -s"
},
]
}
For starters, here is my dev environment:
Windows 7 (although I have the same issue on another machine that is Windows 10)
Python 3.6
Git Bash
Sublime Text 3 (version 3.1.1, Build 3176)
SublimeREPL
In Git Bash, I created a new virtual environment:
$ mkdir ~/.venv
$ cd ~/.venv
$ python -m venv test-env
To activate that virtual environment, I use:
$ source ~/.venv/test-env/Scripts/activate
NOTE: I had to modify the activate script (NOT activate.bat) to get the venv to activate properly. Specifically, I changed line 40 which looked something like:
VIRTUAL_ENV="C:\Users\my_user_name\.venv\test-env"
to
VIRTUAL_ENV="/c/Users/my_user_name/.venv/test-env"
Now, when I am in the test-env virtual environment (as evidenced by the "(test-env)" text in Git Bash), I can do the usual stuff like
(test-env)
$ pip install numpy
I also have the SublimeREPL package installed in Sublime Text 3. I setup a new build system (SublimeREPL-python.sublime-build) that looks like:
{
"target": "run_existing_window_command",
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
Now, suppose I have a script
# test.py
import numpy as np
print('numpy was imported without error')
I can type Ctrl+Shift+B, then start typing 'repl', which autoselects the SublimeREPL-python build, then hit Enter. The SublimeREPL appears, but generates an error:
Traceback (most recent call last):
File "test.py", line 2, in <module>
import numpy as numpy
ModuleNotFoundError: No module named 'numpy'
>>>
SublimeREPL was called without using my virtual environment, which causes it to throw the error because numpy wasn't installed in my global python environment.
How can I run my Python script from Sublime Text 3 using SublimeREPL and accessing my virtual environment that was created using venv?
FWIW, I already tried creating a Sublime Project for this code and adding the following to the .sublime-project file:
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "test-env",
"selector": "source.python",
"shell_cmd": "\"C:\\Users\\my_user_name\\.venv\\test-env\\Scripts\\python\" -u \"$file\""
}
]
This allowed me to type Ctrl+Shift+B, then "test-env", then Enter (to build with the test-env build system I just created), which worked as expected and ran without error. However, it does not use SublimeREPL, which I'd like so that I can debug my code (which is more complicated that the simple test script I posted above!) and explore the variables in the REPL rather than just running code in the console.
I know it's not an answer, but a partial solution that might help anyone else on same situation as I am. Also because SublimeREPL support is almost nothing.
Note: This solution requires to modify Main.sublime-menu for every environment and assumes SublimeREPL runs interactively already (adding the "-i" for execution).
Browse packages and open SublimeREPL/config/python/Main-sublime-menu.
Search for line with "id": "repl_python_run".
Duplicate the nearest content inside the curly brackes.
Basically pasting after the same end bracket the following (note the "-i" option on "cmd" to run interactively):
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-u", "-i", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
Change the id repl_python_run to whatever you like, e.g., repl_my_first_env_python.
Change python commmand from "cmd": ["python", "-u", "-i", "$file_basename"] to use wherever your python's virtual environment executable is, e.g., /home/me/.virtualenvs/my_first_env/bin/python (linux example). Save the file.
Edit project to include inside the square brackets from "build_systems" the following:
(If you have no project, create a new Build System and paste next block of code)
{
"name": "my first env",
"target": "run_existing_window_command",
"id": "repl_my_first_env_python",
"file": "config/Python/Main.sublime-menu"
},
Finally Save.
You'll see when Ctrl+Shift+B you'll see my first env as an option.
I want to run python .py file in Visual Studio Code using Windows bash console.
What I tried to do:
Change default shell in settings.json:
{
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
}
Add task in tasks.json to run python command with file name as an argument:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python",
"isShellCommand": true,
"showOutput": "always",
"tasks": [
{
"taskName": "Run python in bash",
"suppressTaskName": true,
"args": ["${file}"]
}
]
}
There are a few problems to solve here:
Tasks are not being run in bash as I wanted
To access C drive I need to replace C:\ with /mnt/c in file path
Can you share with my solutions to those problems?
I don't have Windows 10 with bash but I'd imagine the problem is that you're not actually trying to run Python. You're trying to run bash (and then run python). Try setting the command to bash with params ["python", "$file"].