Spaces:
Running
Running
File size: 2,808 Bytes
87c3140 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import os, sys
import win32com.client
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageEnhance
def create_shortcut():
# Request user's confirmation
confirmation = input("Do you want to create a shortcut for the VoucherVision? (y/n): ")
if confirmation.lower() != "y":
print("Okay, no shortcut will be created.")
return
# Get the script path
script_path = os.path.abspath(__file__)
# Get the directory of the script
script_dir = os.path.dirname(script_path)
# Path to the icon file
icon_path = os.path.join(script_dir, 'img', 'icon.jpg')
img = Image.open(icon_path)
enhancer = ImageEnhance.Color(img)
img_enhanced = enhancer.enhance(1.5)
img_enhanced.save(os.path.join(script_dir, 'img', 'icon.ico'), format='ICO', sizes=[(256,256)])
icon_path_ico = os.path.join(script_dir, 'img', 'icon.ico')
# Construct the path to the static folder
static_dir = os.path.join(script_dir, "static")
# Ask for the name of the shortcut
shortcut_name = "Voucher Vision"
root = tk.Tk()
root.withdraw() # Hide the main window
root.update() # Ensures that the dialog appears on top
folder_path = filedialog.askdirectory(title="Choose location to save the shortcut")
print(f"Shortcut will be saved to {folder_path}")
venv_path = filedialog.askdirectory(title="Choose the location of your Python virtual environment")
print(f"Using virtual environment located at {venv_path}")
# Path to the activate script in the venv
activate_path = os.path.join(venv_path, "Scripts")
shortcut_path = os.path.join(folder_path, f'{shortcut_name}.lnk')
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
shortcut.Targetpath = "%windir%\System32\cmd.exe"
# The command activates the venv, navigates to the script's directory, then runs the script
# shortcut.Arguments = f'/K "{activate_path} & cd /D {os.path.dirname(script_path)} & streamlit run VoucherVisionEditor.py"'
# shortcut.Arguments = f'/K "{activate_path} & cd /D {static_dir} & start cmd /c python -m http.server & cd /D {script_dir} & streamlit run VoucherVisionEditor.py"'
streamlit_exe = os.path.join(venv_path, "Scripts","streamlit")
print(script_dir)
print(streamlit_exe)
activate_path = os.path.join(script_dir,"venv_VV","Scripts")
print(activate_path)
shortcut.Arguments = f'/K cd /D ""{activate_path}"" && activate && cd /D ""{script_dir}"" && python run_VoucherVision.py'
# Set the icon of the shortcut
shortcut.IconLocation = icon_path_ico
shortcut.save()
print(f"Shortcut created with the name '{shortcut_name}' in the chosen directory.")
if __name__ == "__main__":
create_shortcut() |