Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量从Javascript提取到Python函数_Javascript_Python_Html_Google Maps_Pyqt5 - Fatal编程技术网

将变量从Javascript提取到Python函数

将变量从Javascript提取到Python函数,javascript,python,html,google-maps,pyqt5,Javascript,Python,Html,Google Maps,Pyqt5,我正在使用PyQt5创建一个小应用程序。在某个时候,我需要让用户从地图上选择纬度和经度。经过一些研究,实现地图搜索的最简单方法似乎是通过一些Javascript行使用GoogleMaps。 我发现这段代码几乎满足了我的需要: 但是,我需要在python函数中使用用户选择的坐标。如何将Javaascript代码(“latLng”)中选择的纬度和经度传递给python?我在很多论坛上看到了很多类似的问题,但我不知道如何解决我的问题。我希望找到一个非常简单的解决方案。 我的代码类似于: def la

我正在使用PyQt5创建一个小应用程序。在某个时候,我需要让用户从地图上选择纬度和经度。经过一些研究,实现地图搜索的最简单方法似乎是通过一些Javascript行使用GoogleMaps。 我发现这段代码几乎满足了我的需要:

但是,我需要在python函数中使用用户选择的坐标。如何将Javaascript代码(“latLng”)中选择的纬度和经度传递给python?我在很多论坛上看到了很多类似的问题,但我不知道如何解决我的问题。我希望找到一个非常简单的解决方案。 我的代码类似于:

def latlong_function():

        html='''
 <html>  <head> ....  ....
'''

print(coordinates)

return html
def latlong_函数():
html=“”
....  ....
'''
打印(坐标)
返回html

在为web应用程序开发python后端方面,我不是专家,但我确实有在web环境中使用PHP和perl的经验

除非python有一个特定的实用程序/库来完成这项工作(根据您的研究,它听起来好像没有),否则最好的办法是让JavaScript通过HTTP POST请求提交数据,使用类似的方式

类似的方法可能会奏效:

// Function called when an HTML button with id of "Search_Button" is clicked
$('#Search_Button').click(function () {

   // Get the values of your lat/long HTML inputs
   var lat = $('#Latitude_Input').val();
   var long = $('#Longitude_Input').val();

   // Submit the AJAX request
   $.ajax({
     url: "path/to/your_script.py", // Path of the python script to do the processing
     method: "POST", // Method to submit the HTTP request as, usually either POST or GET
     data: { // These are the POST parameters passed to the backend script
      latitude: lat,
      longitude: long 
     },
     success: function (returned_data) { // Function to run if successful, with the function parameter being the output of the url script
       alert('Here is the output: ' + returned_data);
     },
     error: function () { // Function to run if unsuccessful
       alert('An error occured');
     }
   });
});
这应该允许您从HTML页面获取数据,并让python脚本进行处理


用于处理python中的POST/GET请求。

我建议从Javascript向python代码发送AJAX请求。然后python资源可以返回您所需的值。非常简单,真的:只需将google.maps加载到中,然后与回调一起使用以获取值。这与我正在做的非常接近,但是如何获得回调呢?你有什么示例代码可以发布吗?@CarloBianchi。然后,它可以接收javascript函数的返回值。如果返回值是一个复杂的对象,可以使用json对其进行字符串化。我知道回调是什么,但如何“接收javascript函数的返回值”。我的意思是,问题是:如何将信息从Javascript传递到Python?在这一点上,AJAX选项或类似选项似乎是唯一的选择。