网站开发工程师累不累,12316网站建设方案,单一产品做网站,网络推广自学windows一键启动jupyter 
jupyter简介 
Jupyter是一个开源的交互式计算环境#xff0c;主要用于数据分析、数据可视化和科学计算。它的名字来源于三种编程语言的缩写#xff1a;Julia、Python和R#xff0c;这三种语言都可以在Jupyter环境中运行。如果您想进行数据分析、科学…windows一键启动jupyter 
jupyter简介 
Jupyter是一个开源的交互式计算环境主要用于数据分析、数据可视化和科学计算。它的名字来源于三种编程语言的缩写Julia、Python和R这三种语言都可以在Jupyter环境中运行。如果您想进行数据分析、科学计算、机器学习等任务Jupyter是一个非常有用的工具因为它提供了一个交互式的环境让您可以探索数据、编写代码并实时查看结果。 
Jupyter的主要特点 
交互性 
Jupyter环境允许用户以交互式的方式编写和执行代码。用户可以一次性执行一条代码或一块代码然后立即查看结果。这对于数据分析和探索性编程非常有用。 
支持多种编程语 
虽然最初是以支持Julia、Python和R为主但实际上Jupyter可以支持众多编程语言。用户可以通过安装相应的内核来扩展Jupyter的语言支持。 
Notebook文档 
Jupyter Notebook是Jupyter环境的一种文件格式它允许用户在同一个文档中混合编写可执行的代码、可视化输出、文本说明、公式和图像等内容。这使得Jupyter成为了一个非常适合分享和展示分析过程的工具。 
数据可视化 
Jupyter环境内嵌了丰富的数据可视化工具使用户可以方便地创建图表、图像和动画以更好地理解数据。 
灵活性 
Jupyter可以在本地计算机上安装运行也可以在云端服务器上部署。这使得用户可以根据自己的需求选择适合的运行环境。 
Jupyter 的安装 
在Linux上安装Jupyter Notebook 安装Python 大多数Linux发行版都自带Python您可以通过以下命令检查是否已经安装 python --version如果未安装Python或需要更新版本请根据您的发行版使用适当的包管理工具进行安装或更新。  安装pip 如果未安装pip可以使用以下命令安装 sudo apt-get update
sudo apt-get install python3-pip安装Jupyter Notebook 在终端中运行以下命令来安装Jupyter Notebook python3 -m pip install jupyter启动Jupyter Notebook 在终端中运行以下命令来启动Jupyter Notebook jupyter notebook在Windows上安装Jupyter Notebook 安装Python和pip 可以从Python官方网站https://www.python.org/downloads/windows/下载Python安装程序并确保勾选“Add Python to PATH”选项。  安装Jupyter Notebook 在命令提示符Command Prompt中运行以下命令来安装Jupyter Notebook python -m pip install jupyter启动Jupyter Notebook 在命令提示符中运行以下命令来启动Jupyter Notebook jupyter notebook总结一下不管是linux还是windows安装都是一下几个步骤 1. 首先安装python环境windows则配置一下环境2. 再安装pip环境3. 通过pip命令安装jupyter模块4. 运行jupyter如何在windows下一键启动安装jupyter? 
背景为什么要这么做 
实际情况是我们想做一个在线的python编辑器需要用户能再浏览器进行在线编程python并且能实时输出python代码执行的结果。有人可能会说为什么不直接通过代码进行执行服务器上的python命令来执行python代码呢但是这样其实做不到实时返回执行结果的比如一个for循环打印无法实现比如用户需要输入接受输入以后展示输出结果。 
设计思路怎么做 
问题 
windows执行意见安装一般都是一个exe程序点击一键启动如何打包exe启动如何安装python和pip的环境 
解决问题 
目前打包exe的方式一般有两种 使用 pyinstaller 打包 Python 程序为 .exe和使用 Visual Studio 打包 C# 程序为 .exe这里我们对python代码比较熟悉选择使用pyinstaller首先python有windows的安装包如果通过python代码去安装python.exe体验非常不好需用用户去点击安装。这里我们换个思路找到python官网提供免安装版本的python包。 
不要开心的太早了免安装版本的python包不没有pip模块的所以就得考虑安装pip模块。python安装pip模块见:免安装版本python安装pip模块。 
总结 
我们再总结一下前面的思路进行一个比较详细的设计思路 
下载官方python进行解压并且将 get-pip.py 放入到python文件夹下首先判断python是否已经安装过未安装则配置python 环境变量首先判断pip模块是否已经安装过未安装通过python代码安装pip模块安装完以后pip模块后再配置pip的环境变量为了防止安装jupyter超时通过python代码更换镜像源为国内镜像源使用pip进行安装jupyter启动jupyter 
此步骤则是完整的代码逻辑其实这里并不完整因为你安装了jupyter 你肯定不想每次使用的时候都去点击exe来启动jupyter所以是需要做一个windows的开机自启动的这个后面会降到如何做开机自启。 
代码实现 
jupyter 打包exe 代码实现 
jupyter.py 
import os
import subprocess
import traceback
import socket
from utils.logutils import logpython_path  python
scripts_path  python\Scriptsdef is_installed(model):try:# 尝试运行 python 命令result  subprocess.run([model, --version], stdoutsubprocess.PIPE, stderrsubprocess.PIPE)# 检查返回结果if result.returncode  0:return Trueelse:return Falseexcept FileNotFoundError:return Falsedef install_pip_module():if not is_installed(pip):# 安装 pip 模块try:new_path  os.path.join(os.getcwd(), fpython)subprocess.check_call([python, f{new_path}\get-pip.py])log.info(成功安装 pip 模块)except subprocess.CalledProcessError:log.info(安装 pip 模块时出错)def configure_path(path):# Assuming the path to add is /path/to/python/binnew_path  os.path.join(os.getcwd(), f{path})# 获取当前的PATH环境变量current_path  os.environ[PATH]# 在当前的PATH后追加新路径并使用os.pathsep分隔new_path_value  current_path  os.pathsep  new_path# 更新环境变量os.environ[PATH]  new_path_valuedef configure_path_env_variable():if not is_installed(pip) and is_installed(python):env_var_name  PATHpython_path  os.path.join(os.getcwd(), fpython)scripts_path  os.path.join(os.getcwd(), fpython\Scripts)env_var_value  python_path  os.pathsep  scripts_pathtry:# 设置环境变量configure_path(fpython)configure_path(fpython\Scripts)subprocess.check_call([setx, env_var_name, f%{env_var_name}%;{env_var_value}])log.info(f成功追加环境变量: {env_var_name}{env_var_value})except subprocess.CalledProcessError:log.info(f追加环境变量时出错: {env_var_name})def change_mirror_source():if not is_installed(pip):# 更换镜像源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplemirror_url  https://pypi.tuna.tsinghua.edu.cn/simpleos.system(pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple)log.info(f已更换镜像源{mirror_url})def install_jupyter():if not is_installed(jupyter):# 使用 pip 安装 jupytertry:subprocess.check_call([python, -m, pip, install, jupyter_kernel_gateway])log.info(成功安装 Jupyter)except subprocess.CalledProcessError:log.info(安装 Jupyter 时出错)def start_jupyter():# 启动 Jupyter  start /B jupyter kernelgatewaycmd  [jupyter, kernelgateway, --KernelGatewayApp.ip0.0.0.0, --KernelGatewayApp.port18012,--KernelGatewayApp.allow_credentials*, --KernelGatewayApp.allow_headers*,--KernelGatewayApp.allow_methods*, --KernelGatewayApp.allow_origin*]try:subprocess.Popen(cmd, creationflagssubprocess.CREATE_NEW_CONSOLE, shellTrue)log.info(Jupyter kernel gateway 启动成功)except OSError as error:log.error(error)def is_running():try:s  socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.settimeout(3)result  s.connect_ex((127.0.0.1, 18012))s.close()if result  0:log.info(AI-Link.exe is already running or port conflict!)return Trueelse:return Falseexcept:log.info(Exception of is_running:{0}.format(traceback.format_exc()))return Falsedef main_process():if not is_running():# 配置环境变量configure_path_env_variable()# 安装pip模块install_pip_module()# 更换镜像源change_mirror_source()# 使用pip进行安装jupyterinstall_jupyter()# 启动jupyterstart_jupyter()if __name__  __main__:main_process()logutils.py 
# -*- coding: utf-8 -*-
import time
import win32api
import win32conclass Log(object):def write(self, level, msg):date  time.strftime(%Y-%m-%d, time.localtime())log_file  r{0}{1}{2}.format( jupyter., date, .log)line  {0} {1} {2}\n.format(time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()), level, msg)with open(log_file, a, encodingutf-8) as f:f.write(line)def info(self, msg):self.write(INFO, msg)def error(self, msg):self.write(ERROR, msg)staticmethoddef notice_info(msg: str):win32api.MessageBox(0, f{msg}, 警告, win32con.MB_ICONWARNING)log  Log()jupyter 打包exe 实现windows自启 
思路 
首先要了解windows如何实现开机自启如何通过python代码去打包exe 程序实现开机自启 
解决方案 
一. 要实现在 Windows 开机时自动启动 .exe 程序你可以通过以下几种方式来设置 
将程序快捷方式添加到启动文件夹 按下 Win  R 键打开“运行”对话框。输入 shell:startup然后按回车这将打开当前用户的启动文件夹。在启动文件夹中创建程序的快捷方式可以通过右键点击程序的 .exe 文件然后选择“发送到” - “桌面创建快捷方式”然后将生成的快捷方式拖放到启动文件夹中。 注册表编辑器 按下 Win  R 键打开“运行”对话框。输入 regedit然后按回车打开注册表编辑器。导航到 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run。右键点击右侧的空白处选择“新建” - “字符串值”。将新建的字符串值命名为你想要的名称然后修改其数值数据为程序的完整路径比如 C:\path\to\your\program.exe。 任务计划程序 搜索并打开“任务计划程序”。在左侧导航栏选择“创建基本任务”。按照向导的指示设置任务的名称和触发条件选择“启动程序”作为操作类型。指定要启动的程序的路径然后完成创建任务的过程。  
以上三个方式都可以实现开机自启先看第二个方案需要改注册表理论上代码可行第三个方案需要设计任务计划通过代码实现相对可行性较低再看第一个所有的windows 系统startup文件目录相对固定只需要创建快捷方式即可相对简单如是我们采用方案一。 
二. 通过代码实现开启及自启 
代码实现可行主要问题就是如果不是使用管理员权限执行exe的话就需要强制获取一下admin的管理员权限. 
代码实现 
installer.py 
# -*- coding: utf-8 -*-
import os
import traceback
import ctypes
import sys
import win32com.client as clientshell  client.Dispatch(WScript.Shell)
from utils.logutils import logdef is_admin():try:return ctypes.windll.shell32.IsUserAnAdmin()except Exception as e:log.error(e)return Falsedef create_shortcut():try:filename  os.path.join(os.getcwd(), fjupyter.exe)lnkName  fC:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\jupyter.lnkshortcut  shell.CreateShortCut(lnkName)shortcut.TargetPath  filenameshortcut.save()except:log.info(Exception of creating a shortcut:{0}.format(traceback.format_exc()))def jupyter_installer():if is_admin():create_shortcut()  # 配置自启动服务else:ctypes.windll.shell32.ShellExecuteW(None, runas, sys.executable, __file__, None, 1)if __name__  __main__:try:jupyter_installer()except:log.info(Service initialization failed:{0}.format(traceback.format_exc()))pyinstaller 打包exe 
pyinstaller -F install.pypyinstaller -F jupyter.py源码地址以及打包后的exe文件 
源码地址路径 
https://github.com/LBJWt/windows-jupyter 
打包后的完整exe的文件下载地址 
https://download.csdn.net/download/wagnteng/88245132