【方法】HTML调用本地Python程序

版权声明:偷我的我会气死的, 希望你去访问我的个人主页:crazyang.top https://blog.csdn.net/yzy_1996/article/details/80223053

## 首先声明,在HTML中调用本地Python程序是十分不推荐的

## 但如果你要用,像我一样,需要在课堂上进行一个Python代码的展示,并且想要一个好看的UI界面的话,可以这样来做,做起来还是很容易的,我会手把手教你。

调用本地程序这个权限正在逐渐被取消,目前IE内核还支持,但我用的是Chrome,下载一个IE Tab的拓展程序就可以了。

# 1、编写你的python代码,我就不举例子了

# 2、编写你的HTML代码,这个我把我写的分享出来,其中的重点部分在于java脚本中写的一个函数,你不用改,以及在后面看着像执行这个函数的一部分。如果你要用,有些地址你还是需要修改一下的。

exec1('pythonw facedetection.py')

第一个pythonw是执行的程序软件,pythonw是python一个没有弹框的执行程序,也就是说你用python会先弹出一个命令行的黑框,然后再执行你的后面的代码;用pythonw就不会有黑框出来。第二个是Python代码名称,我这是一个相对地址,启动IE内核后,会在本地产生一个文件夹,我也是在执行时发现的,我的是在C:\Users\Jerry\AppData\Local\IE Tab\11.4.23.1。你也可以使用绝对路径,但要注意Windows下是用的双斜线,不然也是无法执行的。

同时这个里面可以不用这两部分来表达,可以直接用一个exe来执行,这样做的前提是你需要先把你的python程序打包成一个exe文件。

<!DOCTYPE html>  
<html>  
  <head>  
    <meta charset="utf-8">  
	<style>
	#header {
		background-color:black;
		color:white;
		text-align:center;
		padding:5px;
	}
	#nav {
		line-height:50px;
		background-color:#eeeeee;
		height:540px;
		width:220px;
		float:left;
		padding:5px;	      
	}
	#section {
		width:350px;
		float:left;
		padding:7px;	 	 
	}
	#footer {
		background-color:black;
		color:white;
		clear:both;
		text-align:center;
	    padding:1px;	 	 
	}
	</style>	
	<script language="javascript">     
	function exec1(command) 
	{     
	  var ws = new ActiveXObject("WScript.Shell");      
	  ws.run(command);           //exec 和 run
	  ws = null;
	  //alert("ok");             弹出一个小框显示“ok”
	}     
	</script>   
  </head>  
  
<body>
	<div id="header">
	<h1>人脸识别</h1>
	</div>

	<div id="nav">
		人脸检测         
		<a href="facedetection.py" target="show"> 源代码 </a>    
		<input type="button" value="运行 python" onclick="exec1('pythonw facedetection.py')" />  
		<br/> 
	 
		人脸识别    
		<a href="facerecognize.py" target="show"> 源代码 </a>    
		<input type="button" value="运行 python" onclick="exec1('pythonw facerecognize.py')" />  
		<br/>  

		人脸打码    
		<a href="blur_faces.py" target="show"> 源代码 </a>    
		<input type="button" value="运行 python" onclick="exec1('pythonw blur_faces.py')" />  

		<p><b>点击 <U>源代码</U> 将显示代码,
		<br/> 点击 <U>button</U> 将执行代码</b></p>	
		<img src="demo.gif" width="220" height="200" />
		<div id="footer">
	    版权 ©  
	    </div> 
	</div>

	<div id="section"> 	
		<h2>代码框 </h2>
		<iframe name="show" id="show" width="1100" height="466"/>   		 
	</div>

</body> 
</html> 

#3、前面工作都做完了,那就开始运行吧,双击你的HTML文件,就会打开chrome浏览器

点击IE Tab拓展程序,

会弹出这个警告,你需要点击同意

点击运行代码,还会弹出一个警告,你也需要同意

#4、大功告成

有问题欢迎留言

完整代码下载:https://download.csdn.net/download/yzy_1996/10397883

猜你喜欢

转载自blog.csdn.net/yzy_1996/article/details/80223053