如何在python文件中使用JS变量

时间:2018-02-24 15:20:23

标签: javascript python ajax geolocation

大家好日子。我需要帮助尝试从JS获取变量以在我的python代码中使用。我已经浏览了整个谷歌,并尝试使用AJAX和JQuery没有任何运气。我可能只是做得不对。

我的目标是能够使用HTML5 Geolocation来提高准确性并使用我的python代码中的坐标。如果有人能帮助我,那将是非常棒的。感谢。

  

Python代码

from flask import *
from flask_jsglue import JSGlue
app = Flask(__name__)
jsglue = JSGlue(app)
import json

@app.route('/')
def index():
    return render_template('the_new_html.html')

@app.route('/receiver', methods=['GET', 'POST'])
def worker():
    lat = request.form['lat']
    long = request.form['long']
    print(lat)
    print(long)






if __name__ == '__main__':
    app.run(debug=True)
  

HTML文件

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Geo</title>
    <script>
      window.onload = function()
      {

        init();
      }

      function init()
      {
        navigator.geolocation.getCurrentPosition(positionsuccess,
        positionerror);
      }

      function positionsuccess(position)
      {
        var lat = position.coords.latitude;
        var long = position.coords.longitude;
        document.getElementById('lat').innerHTML = lat;
        document.getElementById('long').innerHTML = long;
        event.preventDefault();
      }
      function positionerror(error)
      {
        alert(error.message);
      }


    </script>
    <script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
     </script>
  </head>
  <body>
    <form>
      Lat: <span id='lat'></span>
      Long: <span id='long'></span>
      <button type="submit" name="button">Button</button>
    </form>
    <script>

    </script>

  </body>
</html>

1 个答案:

答案 0 :(得分:0)

corpus_path = "data"
article_paths = [os.path.join(corpus_path,p) for p in os.listdir(corpus_path)]

doc = []
for path in article_paths:
    dp = pd.read_table(path, header=None, encoding='utf-8', quoting=3, error_bad_lines=False)
    doc.append(dp)

在您的Flask服务器上,您可以在此处获取数据:

   <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Geo</title>
        <script>
          window.onload = function()
          {

            init();
          }

          function init()
          {
            navigator.geolocation.getCurrentPosition(positionsuccess,
            positionerror);
          }

          function positionsuccess(position)
          {
            var lat = position.coords.latitude;
            var long = position.coords.longitude;
            document.getElementById('lat').innerHTML = lat;
            document.getElementById('long').innerHTML = long;
            event.preventDefault();
          }
          function positionerror(error)
          {
            alert(error.message);
          }

          function buttonClickHandler() {
            const lat = document.getElementById('lat').textContent;
            const long = document.getElementById('long').textContent;
            fetch('/receiver', , {
               method: 'post',
               headers: {
                  'Accept': 'application/json, text/plain, */*',
                  'Content-Type': 'application/json'
               },
              body: JSON.stringify({lat, long})
               }).then(res=>res.json())
              .then(res => console.log(res));
          }

        </script>
        <script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script type="text/javascript">
         </script>
      </head>
      <body>
        <form>
          Lat: <span id='lat'></span>
          Long: <span id='long'></span>
          <button type="button" name="button" onclick="buttonClickHandler">Button</button>
        </form>
        <script>

        </script>

      </body>
    </html>