当前位置: 首页 > 工具软件 > PythonJS > 使用案例 >

JS向python传输数据

司马俊晖
2023-12-01

JS向python传输数据

相比于python向js传输数据来说,js向python传数据就比较简单。因为我目前的需求是通过点击html界面的一个按钮就可以发送LCM消息,这可以直接将需要穿日的数据通过参数值传递,再通过python进一步编辑即可。

上py代码


#In[]
import time
import sys
from xml.etree.ElementPath import prepare_predicate
import lcm
from lcm_interface import ObuCmdMsg, ObuCmd, ModuleStatus
from lcm_interface import Message,CommCommand,Command,Events,Event,ParkingInfo,ParkingInfoList,Location,Point3D
from lcm_interface import StopInfo,StopPoint,Point3D
from multiprocessing import Process
from threading import Thread
from lcm_interface.ParkingOutInfo import ParkingOutInfo
import eel

#In[]
def pub_ModuleStatus(channel,name,self_id,status_code):
    lc = lcm.LCM("udpm://239.255.76.21:7621?ttl=3")
    moduleStatus = ModuleStatus()

    moduleStatus.name = name
    moduleStatus.self_id = self_id
    moduleStatus.status = status_code
    moduleStatus.messages = []

    lc.publish(channel, moduleStatus.encode())
# Set web files folder and optionally specify which file types to check for eel.expose()
#   *Default allowed_extensions are: ['.js', '.html', '.txt', '.htm', '.xhtml']

#In[]
def pub_ObuCmd(self,channel,code,val):
    lc = lcm.LCM("udpm://239.255.76.21:7621?ttl=3")
    my_ObuCmdMsg = ObuCmdMsg()
    t = time.time()
    t1 = int(t)
    t2 = int((t - t1) * 100000000)
    my_ObuCmdMsg.header.stamp.sec = t1
    my_ObuCmdMsg.header.stamp.nsec = t2
    my_ObuCmdMsg.header.frame_id = self
    
    my_ObuCmdMsg.id = 15
    my_ObuCmdMsg.name = self
    my_ObuCmdMsg.obu_cmd_list_size = 1
    
    my_ObuCmd = ObuCmd()
    my_ObuCmd.code = code
    my_ObuCmd.val = val
    my_ObuCmdMsg.obu_cmd_list.append(my_ObuCmd)
    
    # lc = lcm.LCM(lcm_port)
    lc.publish(channel, my_ObuCmdMsg.encode())

#In[]
# eel.init('web', allowed_extensions=['.js', '.html'])
eel.init('web')
global tt
tt =3
@eel.expose                         # Expose this function to Javascript
def get_value(x):
    # print('Hello from1 ' )
    if x['a']==2:
        pub_ModuleStatus("/driveout/driveout_state_machine/ModuleStatus","driveout_state_machine",19,1901)
    else:
        pub_ModuleStatus("/driveout/driveout_state_machine/ModuleStatus","driveout_state_machine",19,1902)

@eel.expose                         # Expose this function to Javascript
def get_obu(x):
    # print('Hello from1 ' )
    if x['a']==3:
        pub_ObuCmd('driverout','/vui_client/ObuCmdMsg',10042,4)
    else:
        pub_ModuleStatus("/driveout/driveout_state_machine/ModuleStatus","driveout_state_machine",19,1902)


eel.start('test.html',mode='firefox',port=8002)             # Start (this blocks and enters loop)

# %%

这个代码里面的lcm_interface模块可以查看我安装教程里面的网盘资料有哦

接下来上js代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
        <script type="text/javascript" src="/eel.js"></script>
		<script>
			window.onload = function(){
				var btn = document.getElementById('btn');
				var btn2 = document.getElementById('obu');
                var a={'a':1,'b':2}
                var b={'a':3,'b':4}
                console.log(a)
				btn.onclick = function(){
                    console.log(a)
                    eel.get_value(a)
					// alert("hello world");
				}
				btn2.onclick = function(){
                    console.log(b)
                    eel.get_obu(b)
					// alert("hello world");
				}
			}
		</script>
	</head>
	<body>
		<button id="btn">发送model</button>
		<button id="obu">发送obu</button>

	</body>
</html>

**开始你的代码之旅吧 !!! 注意存放lcm_interface文件夹的位置哦,不要放错啦!!! **

 类似资料: