Reputation: 5068
I created a .bat
file that starts conda in my base
environment, then opens Jupyter Notebook. Here's the code:
@echo off
call conda activate base
cd C:\Users\{myUserName}\OneDrive\Documents\Udemy Python Course\My Lesson Scripts
jupyter lab
That all works great. The cmd window opens, I see the "successfully linked/loaded", etc lines in the command window scroll, and a browser tab opens with Jupyter Notebook and everything is honkey dory from there.
But I'd also like to have the cmd window automatically close once the tab is open in the browser so I added
exit
at the end of the bat file but the cmd window didn't close. So to see if the bat script was even getting there I added
echo Closing...
timeout 10 >nul
before the exit
line.
After making sure the Jupyter Notebook tab was open in my browser, I checked the cmd window and "Closing..." was not at the bottom of the window, so it's not getting to that point.
I then went to Jupyter Notebook and in the File menu clicked "Shut Down", quickly brought up the open cmd window and sure enough, "Closing..." showed and in 10 seconds the cmd window closed.
So, the batch file is actually waiting for the server/(service?) to stop before it can close on its own.
I know that with Jupyter Notebook open and I'm doing stuff in there - running cells, etc, closing the cmd windows has no effect on the operation of Jupyter, so I'm wondering if there is a way to "bypass" the cmd window/batch file waiting for the server to shut down and force-close the cmd window from the batch file myself.
Upvotes: 0
Views: 174
Reputation: 49126
The batch file solution involves using the start command:
@echo off
call conda.bat activate base
cd /D "%USERPROFILE%\OneDrive\Documents\Udemy Python Course\My Lesson Scripts"
start "Jupyter" jupyter.exe lab
There could be used also:
@echo off
call conda.bat activate base
start "Jupyter" /D "%USERPROFILE%\OneDrive\Documents\Udemy Python Course\My Lesson Scripts" jupyter.exe lab
It would be best if conda.bat
and jupyter.exe
would be specified with their fully qualified file names enclosed in "
on containing a space or one of these characters &()[]{}^=;!'+,`~
as in this case the Windows Command Processor does not need to search for these two files in the file system using the environment variables PATH
and PATHEXT
.
It would be much better using a shortcut instead of a batch file which can be created as follows.
Start Windows File Explorer and browse to directory %SystemRoot%\System32
(Windows system directory) which is usually C:\Windows\System32
. Click with secondary (usually right) pointing device (mouse) button on the file cmd.exe
to open the context menu for the Windows Command Processor which is processing batch files like the above one.
Click with primary (usually left) pointing device button on the item Show more options on Windows 11 and click next on any Windows in submenu Send to on the context menu item Desktop (create shortcut). Then minimize Windows File Explorer window or press Win+D (Windows log key pressed and hold while pressing additionally key D) to see the desktop with the new shortcut file with name cmd.exe
or cmd.exe - Shortcut
depending on version of Windows. This file has the file extension .lnk
which is not displayed on the Windows desktop.
Click with secondary pointing device button on the newly created shortcut file and (after clicking on Show more options on Windows 11) click on context menu item Rename to change the name of the shortcut file to something meaningful like Jupyter Lab
. The file name with not displayed file extension is then Jupyter Lab.lnk
.
Click with secondary pointing device button on the newly created and already renamed shortcut file and click on the context menu item Properties to open the Properties dialog window for the shortcut.
Modify the property Target by appending a space character and /D /S /C "conda.bat activate base & start "Jupyter" jupyter.exe lab"
. It would be again best if conda.bat
and jupyter.exe
would be specified with their fully qualified file names enclosed in "
on containing a space or one of these characters &()[]{}^=;!'+,`~
.
Next modify the property Start in to %USERPROFILE%\OneDrive\Documents\Udemy Python Course\My Lesson Scripts
. Please note that there are no double quotes around the directory path because of this directory path string is passed after expansion of the environment variable reference %USERPROFILE%
to the Windows kernel library function CreateProcess called by explorer.exe
on using the shortcut file with the function parameter lpCurrentDirectory
. The directory path string is not passed as an argument string to an executable. The Windows kernel function CreateProcess
is also called by cmd.exe
on execution of its internal command start
for starting jupyter.exe
as parallel executed process.
There should be selected for property Run the option Minimized as that results on execution of the shortcut in starting the console application cmd.exe
with a minimized console window which is closed once the batch file is processed and jupyter.exe
is started as separate, parallel executed, detached process.
There should be entered also a meaningful Comment like Starts Jupyter Lab for Lessons
. The comment is shown on hovering the pointing device pointer over the shortcut.
There can be selected a nice icon for the shortcut like one inside of jupyter.exe
if it is a GUI application with one or more icon resources.
There could be lots of other properties defined for the console and its window which is opened by CreateProcess
for the console application cmd.exe
, but that does not make much sense here as the console window is opened minimized just for a very short time.
The property Shortcut key could be defined with Ctrl+Alt+J for the execution of the command line specified with Target starting finally Jupyter Lab by pressing this key combination at any time from within any window.
Click on button OK and the shortcut is ready for usage.
It can be seen very easily that the usage of a shortcut file instead of a batch file is much better for this task as lots of properties passed to CreateProcess
can be defined in the properties of the shortcut which cannot be defined on using a batch file resulting in calling also CreateProcess
on execution with a double click on it.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
cd /?
cmd /?
echo /?
start /?
Upvotes: 0