This example demonstrates the creation of a custom flow element. In this case the flowElement takes the results of a client side form collecting date of birth, setting this as evidence on a flowdata object to calculate a person's starsign. The flowElement also serves additional JavaScript which gets a user's geolocation and saves the latitude as a cookie. This latitude is also then passed in to the flowdata to calculate if a person is in the northern or southern hemispheres.
44 if (month == 1
and day <= 20)
or (month == 12
and day >= 22):
47 elif (month == 1
and day >= 21)
or (month == 2
and day <= 18):
50 elif (month == 2
and day >= 19)
or (month == 3
and day <= 20):
53 elif (month == 3
and day >= 21)
or (month == 4
and day <= 20):
56 elif (month == 4
and day >= 21)
or (month == 5
and day <= 20):
59 elif (month == 5
and day >= 21)
or (month == 6
and day <= 20):
62 elif (month == 6
and day >= 22)
or (month == 7
and day <= 22):
65 elif (month == 7
and day >= 23)
or (month == 8
and day <= 23):
68 elif (month == 8
and day >= 24)
or (month == 9
and day <= 23):
71 elif (month == 9
and day >= 24)
or (month == 10
and day <= 23):
74 elif (month == 10
and day >= 24)
or (month == 11
and day <= 22):
77 elif (month == 11
and day >= 23)
or (month == 12
and day <= 21):
81 class AstrologyFlowElement(FlowElement):
85 super(AstrologyFlowElement, self).__init__()
87 self.datakey =
"astrology" 94 "description":
"the user's hemisphere" 98 "description":
"the user's starsign" 101 "type":
'javascript',
102 "description":
"JavaScript used to get a user's latitude" 109 def process_internal(self, flowdata):
115 dateOfBirth = flowdata.evidence.get(
"query.dateOfBirth")
119 dateOfBirth = dateOfBirth.split(
'-')
121 month = int(dateOfBirth[1])
122 day = int(dateOfBirth[2])
129 result[
"getLatitude"] =
""" 130 navigator.geolocation.getCurrentPosition(function(position) { 131 document.cookie = \"latitude=\" + position.coords.latitude; 132 console.log(document.cookie) 138 latitude = flowdata.evidence.get(
"cookie.latitude")
143 result[
"hemisphere"] =
"Northern" if float(latitude) > 0
else "Southern" 145 data = ElementDataDictionary(self, result)
147 flowdata.set_element_data(data)
149 def get_evidence_key_filter(self):
151 get_evidence_key_filter - A filter (in this case a basic list) stating which evidence 152 the flowElement is interested in 154 return BasicListEvidenceKeyFilter([
"cookie.latitude",
"query.dateOfBirth"])
159 javascript_builder_settings = {
165 myPipeline = (PipelineBuilder({
"javascript_builder_settings": javascript_builder_settings})).add(AstrologyFlowElement()).build()
167 from flask
import Flask, request
169 app = Flask(__name__)
173 @app.route(
'/json', methods=[
'POST'])
177 flowdata = myPipeline.create_flowdata()
182 flowdata.evidence.add_from_dict(
webevidence(request))
190 return json.dumps(flowdata.jsonbundler.json)
196 flowdata = myPipeline.create_flowdata()
201 flowdata.evidence.add_from_dict(
webevidence(request))
211 output +=
"<h1>Starsign</h1>" 213 output +=
"<form><label for='dateOfBirth'>Date of birth</label><input type='date' name='dateOfBirth' id='dateOfBirth'><input type='submit'></form>" 217 if (flowdata.astrology.starSign):
218 output +=
"<p>Your starsign is " + flowdata.astrology.starSign +
"</p>" 220 output +=
"<div id='hemispheretext'>" 222 if (flowdata.astrology.hemisphere):
223 output +=
"<p>Look at the " + flowdata.astrology.hemisphere +
" hemisphere stars tonight!</p>" 237 output += flowdata.javascriptbuilder.javascript
240 const loadHemisphere = function() { 241 fod.complete(function (data) { 242 if(data.astrology.hemisphere) { 243 var para = document.createElement("p"); 244 var text = document.createTextNode("Look at the " + 245 data.astrology.hemisphere + " hemisphere stars tonight"); 246 para.appendChild(text); 248 var element = document.getElementById("hemispheretext"); 249 var child = element.lastElementChild; 251 element.removeChild(child); 252 child = element.lastElementChild; 254 element.appendChild(para); 261 output +=
"</script>"