Skip to content

Commit 41f9263

Browse files
anssakthitusharbana-ansyspre-commit-ci[bot]RobPasMue
authored
Added vscode launch to Manage Python Environments tab (#253)
Co-authored-by: Tushar Bana <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Roberto Pastor Muela <[email protected]>
1 parent 3f77322 commit 41f9263

File tree

5 files changed

+214
-9
lines changed

5 files changed

+214
-9
lines changed

doc/source/installer.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ On the ``Launching options`` section, the following options are available:
334334

335335
* ``Launch Console``: this option starts a console window with the command ``python`` pointing
336336
towards your selected Python environment.
337+
* ``Launch VSCode``: this option starts a ``Visual Studio Code``. If ``Visual Studio Code`` is
338+
not installed, then the ``Ansys Python Manager`` provides instructions to install it.
337339
* ``Launch JupyterLab``: this option starts a ``JupyterLab`` session. If ``JupyterLab`` is
338340
not installed, then the ``Ansys Python Manager`` installs it for you.
339341
* ``Launch Jupyter Notebook``: this option starts a ``Jupyter Notebook`` session. If

setup.nsi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ OutFile "dist\${OUTFILE_NAME}"
2222
!insertmacro MULTIUSER_PAGE_INSTALLMODE
2323
!insertmacro MUI_PAGE_LICENSE "${LICENSE_FILE}"
2424
!insertmacro MUI_PAGE_INSTFILES
25-
!insertmacro MUI_LANGUAGE English
2625
!include "uninstall.nsi"
2726

27+
Function CreateDesktopShortCut
28+
CreateShortCut "$desktop\Ansys Python Manager.lnk" "$INSTDIR\Ansys Python Manager.exe"
29+
FunctionEnd
30+
31+
!define MUI_FINISHPAGE_RUN "$INSTDIR\Ansys Python Manager.exe"
32+
!define MUI_FINISHPAGE_SHOWREADME
33+
!define MUI_FINISHPAGE_SHOWREADME_TEXT "Create Desktop Shortcut"
34+
!define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED
35+
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION "CreateDesktopShortCut"
36+
!insertmacro MUI_PAGE_FINISH
37+
2838
Function .onInit
2939
!insertmacro MULTIUSER_INIT
3040
FunctionEnd
@@ -58,12 +68,6 @@ Section "Ansys Python Manager" SEC01
5868

5969
WriteUninstaller "$INSTDIR\uninstall.exe"
6070

61-
; start after install
62-
Exec "$INSTDIR\Ansys Python Manager.exe"
63-
64-
; After installation succeeded, wait and close
65-
Sleep 5000
66-
Quit
6771
SectionEnd
6872

6973
; Define the uninstaller section
@@ -74,6 +78,7 @@ Section "Uninstall" SEC02
7478
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
7579
Delete "$SMPROGRAMS\Ansys Python Manager\Ansys Python Manager.lnk"
7680
RMDir "$SMPROGRAMS\Ansys Python Manager"
81+
Delete "$desktop\Ansys Python Manager.lnk"
7782
SectionEnd
7883

7984
Icon "dist\ansys_python_manager\_internal\assets\pyansys_icon.ico"
@@ -90,3 +95,4 @@ FunctionEnd
9095
; Call the MUI2 OneClick plugin
9196
!insertmacro MUI_UNPAGE_CONFIRM
9297
!insertmacro MUI_UNPAGE_INSTFILES
98+
!insertmacro MUI_LANGUAGE English

src/ansys/tools/installer/configure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636

3737
class Configure(QtWidgets.QWidget):
38-
"""Manage Virtual Environment w.r.t Python versions tab."""
38+
"""Configure tab."""
3939

4040
def __init__(self, parent):
41-
"""Initialize this tab."""
41+
"""Initialize this class."""
4242
try:
4343
super().__init__()
4444
self._parent = parent

src/ansys/tools/installer/installed_table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
run_linux_command,
4747
run_linux_command_conda,
4848
)
49+
from ansys.tools.installer.vscode import VSCode
4950

5051
ALLOWED_FOCUS_EVENTS = [QtCore.QEvent.Type.WindowActivate, QtCore.QEvent.Type.Show]
5152
LOG = logging.getLogger(__name__)
@@ -251,6 +252,10 @@ def __init__(self, parent=None):
251252
self.button_launch_cmd.clicked.connect(self.launch_cmd)
252253
hbox.addWidget(self.button_launch_cmd)
253254

255+
self.button_launch_cmd = QtWidgets.QPushButton("Launch VSCode")
256+
self.button_launch_cmd.clicked.connect(self.launch_vscode)
257+
hbox.addWidget(self.button_launch_cmd)
258+
254259
self.button_launch_lab = QtWidgets.QPushButton("Launch Jupyterlab")
255260
self.button_launch_lab.clicked.connect(self.launch_jupyterlab)
256261
hbox.addWidget(self.button_launch_lab)
@@ -365,6 +370,10 @@ def launch_jupyterlab(self):
365370
self._update_pck_mnger()
366371
self.launch_cmd(f"python -m jupyter lab || {error_msg}")
367372

373+
def launch_vscode(self):
374+
"""Launch VSCode."""
375+
vscode = VSCode(self)
376+
368377
def launch_jupyter_notebook(self):
369378
"""Launch Jupyter Notebook."""
370379
# handle errors
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
"""VS code launch window."""
24+
import os
25+
26+
from PySide6 import QtCore, QtGui, QtWidgets
27+
28+
from ansys.tools.installer.constants import ANSYS_FAVICON, USER_PATH
29+
30+
31+
class VSCode(QtWidgets.QWidget):
32+
"""VS code launch window."""
33+
34+
def __init__(self, parent):
35+
"""Initialize this class."""
36+
try:
37+
super().__init__()
38+
self._parent = parent
39+
if self.is_vscode_installed():
40+
self._parent.vscode_window = QtWidgets.QWidget()
41+
self._parent.vscode_window.move(
42+
self._parent.vscode_window.frameGeometry().center()
43+
)
44+
vscode_window_label = QtWidgets.QLabel()
45+
vscode_window_label.setText("Configuration")
46+
vscode_window_label.setTextFormat(QtCore.Qt.TextFormat.RichText)
47+
vscode_window_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify)
48+
vscode_window_label.setWordWrap(True)
49+
50+
vscode_layout = QtWidgets.QVBoxLayout()
51+
52+
# Group 1: Configure default Virtual Environment creation path
53+
vscode_window_path_config = QtWidgets.QGroupBox(
54+
"VS Code open directory:"
55+
)
56+
vscode_window_path_config_layout = QtWidgets.QVBoxLayout()
57+
vscode_window_path_config_layout.setContentsMargins(10, 20, 10, 20)
58+
vscode_window_path_config.setLayout(vscode_window_path_config_layout)
59+
60+
# ---> Add box
61+
self.vscode_window_path_config_edit = QtWidgets.QLineEdit()
62+
self.vscode_window_path_config_edit.setText(USER_PATH)
63+
vscode_window_path_config_layout.addWidget(
64+
self.vscode_window_path_config_edit
65+
)
66+
67+
self.vscode_warning_text = QtWidgets.QLabel()
68+
self.vscode_warning_text.setAlignment(
69+
QtCore.Qt.AlignmentFlag.AlignJustify
70+
)
71+
self.vscode_warning_text.setWordWrap(True)
72+
vscode_window_path_config_layout.addWidget(self.vscode_warning_text)
73+
74+
# Finally, add all the previous widgets to the global layout
75+
vscode_layout.addWidget(vscode_window_path_config)
76+
77+
vscode_window_button_open = QtWidgets.QPushButton("Open")
78+
vscode_window_button_open.clicked.connect(
79+
lambda x: self._pop_up("Do you want to open?", self._open_vscode)
80+
)
81+
vscode_window_button_close = QtWidgets.QPushButton("Close")
82+
vscode_window_button_close.clicked.connect(
83+
lambda x: self._pop_up("Do you want to close?", self._close_all)
84+
)
85+
86+
vscode_window_layout_1 = QtWidgets.QHBoxLayout()
87+
vscode_window_layout_1.addWidget(vscode_window_label)
88+
vscode_window_layout_2 = QtWidgets.QHBoxLayout()
89+
vscode_window_layout_2.addWidget(vscode_window_button_open)
90+
vscode_window_layout_2.addWidget(vscode_window_button_close)
91+
92+
vscode_window_layout = QtWidgets.QVBoxLayout()
93+
vscode_window_layout.addLayout(vscode_window_layout_1)
94+
vscode_window_layout.addLayout(vscode_layout)
95+
vscode_window_layout.addLayout(vscode_window_layout_2)
96+
self._parent.vscode_window.setLayout(vscode_window_layout)
97+
98+
self._parent.vscode_window.setWindowTitle("Configuration")
99+
self._parent.vscode_window.setWindowIcon(QtGui.QIcon(ANSYS_FAVICON))
100+
self._parent.vscode_window.resize(500, 40)
101+
self._parent.vscode_window.show()
102+
else:
103+
msg = QtWidgets.QMessageBox()
104+
msg.setTextFormat(QtCore.Qt.TextFormat.RichText)
105+
msg.warning(
106+
self,
107+
"VS Code Launch Error",
108+
f"Failed to launch vscode. Try reinstalling code by following this <a href='https://code.visualstudio.com/download'>link</a>",
109+
)
110+
111+
except Exception as e:
112+
self._parent.show_error(str(e))
113+
114+
def _open_vscode(self):
115+
"""Open VS code from path."""
116+
# handle errors
117+
path = self.vscode_window_path_config_edit.text().strip()
118+
if os.path.exists(path):
119+
error_msg = "echo Failed to launch vscode. Try reinstalling code by following this link https://code.visualstudio.com/download"
120+
self._parent.launch_cmd(f"code {path} && exit 0 || {error_msg}")
121+
122+
self.user_confirmation_form.close()
123+
self._parent.vscode_window.close()
124+
else:
125+
self.vscode_warning_text.setText(
126+
f"""{path} does not exist. Provide a valid path."""
127+
)
128+
self.vscode_warning_text.setStyleSheet(
129+
"""
130+
color: rgb(255, 0, 0);
131+
"""
132+
)
133+
self.user_confirmation_form.close()
134+
135+
def _close_all(self):
136+
"""Close all the pop-up window."""
137+
self.user_confirmation_form.close()
138+
self._parent.vscode_window.close()
139+
140+
def _pop_up(self, message, call_back):
141+
"""Launch the confirmation pop-up window."""
142+
self.user_confirmation_form = QtWidgets.QWidget()
143+
self.user_confirmation_form.move(
144+
self.user_confirmation_form.frameGeometry().center()
145+
)
146+
user_confirmation_label = QtWidgets.QLabel()
147+
user_confirmation_label.setText(message)
148+
user_confirmation_label.setOpenExternalLinks(True)
149+
user_confirmation_label.setTextFormat(QtCore.Qt.TextFormat.RichText)
150+
user_confirmation_label.setAlignment(QtCore.Qt.AlignmentFlag.AlignJustify)
151+
user_confirmation_label.setWordWrap(True)
152+
153+
user_confirmation_layout_horizontal = QtWidgets.QHBoxLayout()
154+
user_confirmation_yes_button = QtWidgets.QPushButton("Yes")
155+
user_confirmation_yes_button.clicked.connect(call_back)
156+
user_confirmation_no_button = QtWidgets.QPushButton("No")
157+
user_confirmation_no_button.clicked.connect(self.user_confirmation_form.close)
158+
user_confirmation_layout = QtWidgets.QVBoxLayout()
159+
user_confirmation_layout.addWidget(user_confirmation_label)
160+
user_confirmation_layout_horizontal.addWidget(user_confirmation_yes_button)
161+
user_confirmation_layout_horizontal.addWidget(user_confirmation_no_button)
162+
user_confirmation_layout.addLayout(user_confirmation_layout_horizontal)
163+
self.user_confirmation_form.setLayout(user_confirmation_layout)
164+
self.user_confirmation_form.setWindowTitle("Confirmation")
165+
icon = QtGui.QIcon(ANSYS_FAVICON)
166+
self.user_confirmation_form.setWindowIcon(icon)
167+
self.user_confirmation_form.resize(400, 40)
168+
self.user_confirmation_form.setWindowFlag(
169+
QtCore.Qt.WindowCloseButtonHint, False
170+
)
171+
self.user_confirmation_form.show()
172+
173+
def is_vscode_installed(self):
174+
"""Check if VSCode is installed or not.
175+
176+
Returns
177+
-------
178+
bool
179+
Whether VSCode is installed or not.
180+
"""
181+
try:
182+
return_val = os.system("code --version")
183+
if return_val == 0:
184+
return True
185+
else:
186+
return False
187+
except:
188+
return False

0 commit comments

Comments
 (0)