\r\n

51Degrees Location Python  4.4

Reverse Geo-Location services for 51Degrees Pipeline

cloud/web.py

This example is designed to output location data using the client-side evidence feature of the pipeline. This example uses the client-side evidence feature of the Pipeline to get location information from the client. This information is used to populate properties containing JavaScript snippets, which are then bundled together into a single JavaScript block by the 'JsonBundler' and 'JavaScriptBuilder' elements. This JavaScript is then used to obtain the additional evidence from the client and pass it back to the server. The Pipeline running on the server then processes this evidence to populate a new JSON result object that is returned to the client.

This example is available in full on GitHub

1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
4 # Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 #
6 # This Original Work is licensed under the European Union Public Licence
7 # (EUPL) v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # *********************************************************************
22 
23 
33 
34 from fiftyone_location.location_pipelinebuilder import LocationPipelineBuilder
35 from fiftyone_pipeline_core.web import webevidence
36 import json
37 
38 # First create the location pipeline with the desired settings.
39 
40 # You need to create a resource key at https://configure.51degrees.com
41 # and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
42 # Alternatively, add a resource_key environment variable
43 import os
44 if "resource_key" in os.environ:
45  resource_key = os.environ["resource_key"]
46 else:
47  resource_key = "!!YOUR_RESOURCE_KEY!!"
48 
49 if resource_key == "!!YOUR_RESOURCE_KEY!!":
50  print("""
51  You need to create a resource key at
52  https://configure.51degrees.com and paste it into the code,
53  'replacing !!YOUR_RESOURCE_KEY!!
54  To get a resourcekey with the properties used in this example go to https://configure.51degrees.com/GCrtGh1L
55  """)
56 else:
57 
58  # Here we add some callback settings for the page to make a request with extra evidence from the client side, in this case the Flask /json route we will make below
59 
60  javascript_builder_settings = {
61  "endpoint": "/json"
62  }
63 
64  pipeline = LocationPipelineBuilder(resource_key=resource_key, settings={"javascript_builder_settings": javascript_builder_settings}).build()
65 
66  from flask import Flask, request
67 
68  app = Flask(__name__)
69 
70  # First we make a JSON route that will be called from the client side and will return
71  # a JSON encoded property database using any additional evidence provided by the client
72 
73  @app.route('/json', methods=['POST'])
74  def jsonroute():
75 
76  # Create the flowData object for the JSON route
77  flowData = pipeline.create_flowdata()
78 
79  # Add any information from the request (headers, cookies and additional
80  # client side provided information)
81 
82  flowData.evidence.add_from_dict(webevidence(request))
83 
84  # Process the flowData
85  flowData.process()
86 
87  # Return the JSON from the JSONBundler engine
88  return json.dumps(flowData.jsonbundler.json)
89 
90  # Helper function to get a property value if it exists and return
91  # the reason why if it doesn't
92  def getValueHelper(flowdata, engine, propertyKey):
93 
94  engineProperties = getattr(flowdata, engine)
95 
96  propertyValue = getattr(engineProperties, propertyKey)
97 
98  if propertyValue.hasValue():
99  return propertyValue.value()
100  else:
101  return propertyValue.no_value_message()
102 
103  # In the main route we dynamically update the screen's device property display
104  # using the above JSON route
105 
106  @app.route('/')
107  def server():
108 
109  # Create the flowData object for the JSON route
110  flowData = pipeline.create_flowdata()
111 
112  # Add any information from the request (headers, cookies and additional
113  # client side provided information)
114 
115  flowData.evidence.add_from_dict(webevidence(request))
116 
117  # Process the flowData
118 
119  flowData.process()
120 
121  # Generate the HTML
122  output = ""
123 
124  # Add the JavaScript created by the pipeline
125  output += "<script>"
126  output += flowData.javascriptbuilder.javascript
127  output += "</script>"
128 
129 
130  output += "<h1>Client side evidence</h1>"
131 
132  # Print a button which gets the user's location information
133  # This is then sent to the previously specified json endpoint
134  # Data comes back from the request and populates the country in the HTML
135 
136  output += """
137 
138  <p>After you select the "use my location" button below, client side JavaScript will
139  update the country field using this information.</p>
140 
141  <p>Country: <span id=country></span></p>
142 
143  <button type="button" onclick="getLocation()">Use my location</button>
144 
145  <script>
146 
147  let getLocation = function() {
148  fod.complete(function (data) {
149  document.getElementById("country").innerHTML = data.location.country
150  }, 'location');
151  }
152 
153  </script>
154 
155 
156  """
157 
158  return output