I am using this code in a Jupyter notebook to open a Google Map.
import gmaps
with open('apikey.txt') as f:
apikey = f.readline()
f.close
gmaps.configure(api_key = apikey)
coordinates = (35.5, 140)
map = gmaps.figure(center=coordinates, zoom_level=10, layout={'width': '800px', 'height': '600px'} )
map
I want to find the limits of the map with Python 3.6.
It seems this can be done in JavaScript with the getBounds method which give latitude and longitude for the SW and NE corners of the displayed map.
Also, JavaScript seems to allow changes to be tracked with the bounds_changed event.
This is exactly what I want to do but I can't see how in Python.
I have looked through both the gmaps 0.9.0 and googlemaps 4.4.0 plugins with no success.
Anyone done this?
You must use Flask to this solution to work.
pip install flask
Using python-flask create a folder templates in your root project folder. This is a specific behavior from flask, it always lookup html files from templates folder.
And create a app.py to start our flask application.
Your project configuration must contain at least that configuration.
.
├── app.py
├── _data
└── poi.csv
├── _templates
└── index.html
Just get this lat lon from this question and stuffed with some data to be more clear about how to fill the data.
data/poi.csv
dataa,SclId,Latitude,Longitude,datab
dataa1,HAT-0,44.968046,-94.420307,datab1
dataa2,HAT-1,44.33328,-89.132008,datab2
dataa3,HAT-2,33.755787,-116.359998,datab3
app.py
# python version 3.8.2
import os
from flask import Flask, render_template
from dotenv import load_dotenv
load_dotenv()
class Location:
def __init__(self,latitude,longitude,produto):
self.lat = latitude
self.lon = longitude
self.nome = produto
def read_data_from_file(caminho):
lista = list()
with open(caminho) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)#skip headers
for row in csv_reader:
lista.append(Location(row[2], row[3], row[1]))
return lista
app = Flask(__name__)
app.config['API_KEY'] = os.getenv("APIKEY")#load google maps api key
def read_dataset(dataset):
items = list()
for i in dataset:
items.append([i.lat, i.lon, i.nome])
return items
poidata = read_dataset('data/poi.csv')
#app.route('/')
def index():
context = {
"key": app.config['API_KEY'],
"poidata": poidata
}
return render_template('./index.html', poidata=poidata, context=context)
if __name__ == '__main__':
app.run(debug=False)
templates/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flask with gmaps</title>
<style>
#map-canvas {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<div id="map-canvas">
</div>
<script>
const poiarray = JSON.parse('{{ poidata | tojson | safe}}');
fillMapItems = (elements, map) => {
const bounds = new google.maps.LatLngBounds();
elements.forEach((item, index) => {
const marker = new google.maps.Marker({ position: new google.maps.LatLng(item[0], item[1]), title: item[2], map: map });
bounds.extend(marker.position);
})
map.fitBounds(bounds);
}
showMap = () => {
const map = new google.maps.Map(document.getElementById('map-canvas'));
fillMapItems(poiarray, map)
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key={{ context.key }}&callback=showMap">
</script>
</body>
</html>
In this code I use python-dotenv, but its use is completely optional, you can use your method to load the env variable with api key loading from file system.
In this line call the LatLngBounds type from googlemaps, we are putting each item from iteration in this "list", after finish iteration just set the map to a distance to fit all points.
const bounds = new google.maps.LatLngBounds();
If you need some clarifications, please let me know.
Related
I have a view that generates data and streams it in real time. I can't figure out how to send this data to a variable that I can use in my HTML template. My current solution just outputs the data to a blank page as it arrives, which works, but I want to include it in a larger page with formatting. How do I update, format, and display the data as it is streamed to the page?
import flask
import time, math
app = flask.Flask(__name__)
#app.route('/')
def index():
def inner():
# simulate a long process to watch
for i in range(500):
j = math.sqrt(i)
time.sleep(1)
# this value should be inserted into an HTML template
yield str(i) + '<br/>\n'
return flask.Response(inner(), mimetype='text/html')
app.run(debug=True)
You can stream data in a response, but you can't dynamically update a template the way you describe. The template is rendered once on the server side, then sent to the client.
One solution is to use JavaScript to read the streamed response and output the data on the client side. Use XMLHttpRequest to make a request to the endpoint that will stream the data. Then periodically read from the stream until it's done.
This introduces complexity, but allows updating the page directly and gives complete control over what the output looks like. The following example demonstrates that by displaying both the current value and the log of all values.
This example assumes a very simple message format: a single line of data, followed by a newline. This can be as complex as needed, as long as there's a way to identify each message. For example, each loop could return a JSON object which the client decodes.
from math import sqrt
from time import sleep
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def index():
return render_template("index.html")
#app.route("/stream")
def stream():
def generate():
for i in range(500):
yield "{}\n".format(sqrt(i))
sleep(1)
return app.response_class(generate(), mimetype="text/plain")
<p>This is the latest output: <span id="latest"></span></p>
<p>This is all the output:</p>
<ul id="output"></ul>
<script>
var latest = document.getElementById('latest');
var output = document.getElementById('output');
var xhr = new XMLHttpRequest();
xhr.open('GET', '{{ url_for('stream') }}');
xhr.send();
var position = 0;
function handleNewData() {
// the response text include the entire response so far
// split the messages, then take the messages that haven't been handled yet
// position tracks how many messages have been handled
// messages end with a newline, so split will always show one extra empty message at the end
var messages = xhr.responseText.split('\n');
messages.slice(position, -1).forEach(function(value) {
latest.textContent = value; // update the latest value in place
// build and append a new item to a list to log all output
var item = document.createElement('li');
item.textContent = value;
output.appendChild(item);
});
position = messages.length - 1;
}
var timer;
timer = setInterval(function() {
// check the response for new data
handleNewData();
// stop checking once the response has ended
if (xhr.readyState == XMLHttpRequest.DONE) {
clearInterval(timer);
latest.textContent = 'Done';
}
}, 1000);
</script>
An <iframe> can be used to display streamed HTML output, but it has some downsides. The frame is a separate document, which increases resource usage. Since it's only displaying the streamed data, it might not be easy to style it like the rest of the page. It can only append data, so long output will render below the visible scroll area. It can't modify other parts of the page in response to each event.
index.html renders the page with a frame pointed at the stream endpoint. The frame has fairly small default dimensions, so you may want to to style it further. Use render_template_string, which knows to escape variables, to render the HTML for each item (or use render_template with a more complex template file). An initial line can be yielded to load CSS in the frame first.
from flask import render_template_string, stream_with_context
#app.route("/stream")
def stream():
#stream_with_context
def generate():
yield render_template_string('<link rel=stylesheet href="{{ url_for("static", filename="stream.css") }}">')
for i in range(500):
yield render_template_string("<p>{{ i }}: {{ s }}</p>\n", i=i, s=sqrt(i))
sleep(1)
return app.response_class(generate())
<p>This is all the output:</p>
<iframe src="{{ url_for("stream") }}"></iframe>
5 years late, but this actually can be done the way you were initially trying to do it, javascript is totally unnecessary (Edit: the author of the accepted answer added the iframe section after I wrote this). You just have to include embed the output as an <iframe>:
from flask import Flask, render_template, Response
import time, math
app = Flask(__name__)
#app.route('/content')
def content():
"""
Render the content a url different from index
"""
def inner():
# simulate a long process to watch
for i in range(500):
j = math.sqrt(i)
time.sleep(1)
# this value should be inserted into an HTML template
yield str(i) + '<br/>\n'
return Response(inner(), mimetype='text/html')
#app.route('/')
def index():
"""
Render a template at the index. The content will be embedded in this template
"""
return render_template('index.html.jinja')
app.run(debug=True)
Then the 'index.html.jinja' file will include an <iframe> with the content url as the src, which would something like:
<!doctype html>
<head>
<title>Title</title>
</head>
<body>
<div>
<iframe frameborder="0"
onresize="noresize"
style='background: transparent; width: 100%; height:100%;'
src="{{ url_for('content')}}">
</iframe>
</div>
</body>
When rendering user-provided data render_template_string() should be used to render the content to avoid injection attacks. However, I left this out of the example because it adds additional complexity, is outside the scope of the question, isn't relevant to the OP since he isn't streaming user-provided data, and won't be relevant for the vast majority of people seeing this post since streaming user-provided data is a far edge case that few if any people will ever have to do.
Originally I had a similar problem to the one posted here where a model is being trained and the update should be stationary and formatted in Html. The following answer is for future reference or people trying to solve the same problem and need inspiration.
A good solution to achieve this is to use an EventSource in Javascript, as described here. This listener can be started using a context variable, such as from a form or other source. The listener is stopped by sending a stop command. A sleep command is used for visualization without doing any real work in this example. Lastly, Html formatting can be achieved using Javascript DOM-Manipulation.
Flask Application
import flask
import time
app = flask.Flask(__name__)
#app.route('/learn')
def learn():
def update():
yield 'data: Prepare for learning\n\n'
# Preapre model
time.sleep(1.0)
for i in range(1, 101):
# Perform update
time.sleep(0.1)
yield f'data: {i}%\n\n'
yield 'data: close\n\n'
return flask.Response(update(), mimetype='text/event-stream')
#app.route('/', methods=['GET', 'POST'])
def index():
train_model = False
if flask.request.method == 'POST':
if 'train_model' in list(flask.request.form):
train_model = True
return flask.render_template('index.html', train_model=train_model)
app.run(threaded=True)
HTML Template
<form action="/" method="post">
<input name="train_model" type="submit" value="Train Model" />
</form>
<p id="learn_output"></p>
{% if train_model %}
<script>
var target_output = document.getElementById("learn_output");
var learn_update = new EventSource("/learn");
learn_update.onmessage = function (e) {
if (e.data == "close") {
learn_update.close();
} else {
target_output.innerHTML = "Status: " + e.data;
}
};
</script>
{% endif %}
Hello everyone!,
so I was building a flask app that displays the current time.The time does display on the webpage as intended.But,the thing is that I have the reload the page manually for the time to update.But I want the time to update on itself.Here is my python file containing the code to the website:
from flask_sqlalchemy import SQLAlchemy
import time
from flask import render_template , Flask
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
#app.route('/')
def index():
while True:
global time
time1 = time.time()
global time2
time2 = time.ctime(time1)
return render_template('index.html' , time3 = time2)
if __name__ == '__main__':
app.run(debug = True)
and here is my HTML code for the website:
<!DOCTYPE HTML>
<head>
</head>
<body>
<h1>{{time3}}</h1>
</body>
Can anyone help me out plz?
As I know, you can't do this only using html and python, you need to use Javascript or any other client side language. If time info will be served by python(from server) you can do it like ;
Html and JavaScript code:
<!DOCTYPE HTML>
<html>
<body>
<h>Time: </h>
<span id="time"></span>
</body>
<script type=text/javascript>
function timeClock()
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "{{ url_for('time_info') }}", true);
xhr.onload = function()
{
if(this.status = 200)
{
time.innerHTML = this.response;
}
else
{
console.error(xhr);
}
};
xhr.send(null);
}
setInterval(timeClock, 1000);
</script>
</html>
Flask code:
from flask import Flask,render_template,jsonify
import time
app = Flask(__name__)
#app.route("/")
def main_page():
return render_template("index.html")
#app.route("/time_info",methods=['GET'])
def time_info():
return jsonify(time.ctime())
if __name__ == "__main__":
app.run(debug=True)
timeClock() func will send GET reqest to the server every second and python will get this request and it will send time.ctime() output to the client. Client will get this info and then it will update the html span element
I am doing a Flask tutorial for a Restful API and am trying to make the stores variable appear as an alert on the homepage using the following syntax in both files.
Any ideas on why I can't seem to get the alert to pop up in the browser whenever I go to localhost ?
Additionally, I have been told by Stack Overflow that my post does not contain enough words so I am typing more words here.
main.py file
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
stores =[
{
'name':'The Best Store',
'items':[
{
'name': 'Air Jordans',
'price': 16.99
}
]
}
]
#simulating an online store
#app.route('/')
def home():
return render_template('index.html')
#app.route('/store', methods=['POST'])
def create_store():
request_data = request.get_json() #function for handling request from browser to add post request
new_store = {
'name': request_data['name'],
'items': []
}
stores.append(new_store)
return jsonify(new_store)
#app.route('/store/<string:name>')
def get_store(name): #function to check through existing store names and print error if already found in DB ß
for store in stores:
if store['name'] == name:
return jsonify(store)
return("store not found")
#app.route('/store')
def get_stores():
return jsonify({'stores':stores}) #the stores variable above is a list. We need to make iti nto a dictionary for jsonify to turn into a Json.
#app.route('/store/<string:name>/item', methods=['POST'])
def create_item_in_store(name): #check to see if the post method is equal to a store and the append the new names / prices
for store in stores:
request_data = request.get_json()
if store['name'] == name:
new_item = {
'name': request_data['name'],
'price': request_data['price']
}
store['items'].append(new_item)
return jsonify(new_item)
return jsonify({'message': 'store not found'}) #make sure this is outside of the loop ... obvious reasons
#app.route('/store/<string:name>/item')
def get_items_in_store(name):
for store in stores:
if store['name'] == name:
return jsonify({'items': store['items']})
return jsonify({'message': 'store not found'})
app.run(port=5000)
app.debug=True
index.html file
<html>
<head>
<script type="text/javascript">
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
httpGetAsync('http://127.0.0.1:5000/store', function() {
alert(response);
})
</script>
</head>
<body>
<div id="myElement">
Hello, world!
</div>
</body>
</html>
There is a simple flask app which writes statistics-table from db to a page. How can I plot plotly.express chart on this page?
Code for chart that I want to integrate to a flask app: (took from https://plotly.com/python/time-series/)
# Using plotly.express
import plotly.express as px
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/...')
fig = px.line(df, x='Date', y='AAPL.High')
fig.show()
There need to be more answers floating around out there that actually show how to do this without using dash. I will share my working example of using plotly.express and flask. I'll cut out most of the data work and figure building to just show what you need to do.
Imports needed
You'll need these in addition to your usual px and flask imports.
from plotly import utils
from json import dumps
Short Explanation: JSON and Plotly.js are key
I use pandas to get a dataframe in a function called get_data and get a scatter plot with lines connected with a function called get_lfig. The only important thing to note here is that get_lfig is returning a figure generated from px.scatter() but it can be any figure. Now the trick here is to turn your figure into a JSON and use it on the template side somewhere, you don't do much in python.
Example 1: Create the fig, JSON and pass it to the template
Altogether it looks something like this
from flask import Flask, render_template
from extensions import get_data, get_lfig
from plotly import utils
from json import dumps
app = Flask(__name__)
#app.route('/')
def home():
# function that get data, private and public sets
days=2
priv_data, pub_data= get_data(hours=24*days)
# function that returns a px.scatter figure
all_pub_fig = get_lfig(pub_data)
# turn the figure into a JSON then pass it to the template
all_pub_json = dumps(all_pub_fig, cls=utils.PlotlyJSONEncoder)
return render_template('graph.html', pub_lines_JSON=all_pub_json)
if __name__ == '__main__':
app.run(debug=True, port=8080, host='0.0.0.0')
On the template side, you want to include the src for plotly.js somewhere and then just use the Plotly.plot() function to populate your graph in a div.
<!-- somewhere up top -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
...
<h3>Line Graph Representation</h3>
<!-- scatter plot goes in this div -->
<div id='all-pub-graph'></div>
<script type="text/javascript">
// here is where the JSON gets plugged in via JS
var the_pubs_graph = {{pub_lines_JSON | safe}};
// you target the graph div id in the first arg,
// put your graph in the second, and set the third as {}
Plotly.plot("all-pub-graph", the_pubs_graph, {});
</script>
Example 2: Create the JSON, send it to JS fetch request
Knowing you just want a JSON of your figure, you can take this further and handle it in all JS without having to pass it to the template directly.
Here I handle a post request from on-page selectors that make an api call every time they're changed. The get_lfig function now returns a JSON of the figure instead of a figure object.
#app.route('/get-graphs', methods=['POST'])
def get_graphs():
if request.method == 'POST':
# This whole block is just form handling and data stuff
form = dict(request.form)
agg_func = form['agg_func']
days = int(form['days'])
interval = int(form['interval'])
if (len(agg_func) > 0) and (interval != 0):
pub, priv = get_data(hours=24*days, interval=interval, agg_func=agg_func)
else:
pub, priv = get_data(hours=24*days)
# get JSON figures from the data
pub_f, priv_f = get_lfig(pub), get_lfig(priv)
return {'public': pub_f, 'private': priv_f}
On the template side I use an event listener attached to a form so every time I make a change the graphs get updated. I still need the JS function to be able to find the url, so I pass it to the function using url_for() since utils.js isn't being rendered and can't take advantage of that same template functionality.
<div id="selectors">
<form id="graph-selectors" method="post" onchange="selector_changed('{{url_for('get_graphs')}}')">
<label for="day-selector">Days</label>
<select name="days" id="day-selector">
{% for opt in range(1,10) %}
<option value={{opt}}>{{opt}}</option>
{% endfor %}
</select>
<label for="function-selector">Aggregate Func</label>
<select name="agg_func" id="function-selector">
{% for opt in ['','mean','sum','min','max','std','count'] %}
<option value="{{opt}}">{{opt}}</option>
{% endfor %}
</select>
<label for="interval-selector">Aggregate Interval</label>
<select name="interval" id="interval-selector">
{% for opt in [0, 5, 10, 30, 60] %}
<option value={{opt}}>{{opt}} minutes</option>
{% endfor %}
</select>
</form>
</div>
<div id="content">
<div id="graph-section"></div>
</div>
<script src="{{ url_for('static', filename='utils.js') }}"></script>
then finally, the selector_changed function that makes an API call is stored in my utils.js and looks like this
// I just use this so it clears existing graphs between changes
const clearChildren = (parent) => {
while (parent.lastChild) {
parent.removeChild(parent.lastChild);
}
}
async function selector_changed(gUrl) {
// get the form data
var form_data = new FormData(document.querySelector('form#graph-selectors'))
// send it to get_graphs()
let response = await fetch(gUrl, {
method: "POST",
body: form_data
});
// get_graphs() returns the figure's JSON
let graphJSONs = await response.json();
// declare and clear the target area graphs will go in
var target_area = document.getElementById('graph-section');
clearChildren(target_area);
// create the public server graph section
var pub_area = document.createElement('div');
var pub_header = document.createElement('h3');
var pub_graph = JSON.parse(graphJSONs['public']);
pub_header.textContent = "Public Servers";
pub_area.id = 'public-graphs';
// create the private server graph section
var priv_area = document.createElement('div');
var priv_header = document.createElement('h3');
var priv_graph = JSON.parse(graphJSONs['private']);
priv_header.textContent = "Internal Servers";
priv_area.id = 'private-graphs';
// add everything to the page
target_area.append(pub_area); // start w/the divs
target_area.append(priv_area);
Plotly.plot(pub_area.id, pub_graph, {}); // then add the graphs
Plotly.plot(priv_area.id, priv_graph, {});
pub_area.prepend(pub_header); // then add the headers
priv_area.prepend(priv_header);
}
This is a lot of code, but I wanted to show two ways to handle this which are:
Creating a JSON and passing it to the template directly, and
Handling it as an API call that responds to fetch requests.
The second option is faster and you don't have to refresh the entire page every time, the first option was just showing how to do it with as little code as possible. Either way there should be enough here to modify to your needs and for my future reference. (:
i think to do this is a little more complicated to just call a fig.show(), take a fast look in the ploty lib i found this packet import dash_html_components as html with this you can return a html with your chart to put in web site,
from flask import Flask
app = Flask(__name__)
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
#app.route('/chart')
def chart():
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/...')
fig = px.line(df, x='Date', y='AAPL.High')
return html.Div([dcc.Graph(figure=fig)])
I have a pyramid application running under apache using mod_wsgi.
I am planning to migrate from apache to cherrypy.
I am able to load static page of the existing web application with cherrypy. However for any AJAX request, I am getting resource not found (404) error.
Any clues??
Thanks
30-Mar-2016
Here is code structure
MyProject
|
cherry_wsgi.py (creates wsgi app object)
cherry_server.py (starts cherrypy server using app object from cherry_wsgi.py)
development.ini
myproject
|
__init__.py (Scans sub-folders recursively)
views.py
mydata
|
__init__.py
data
|
__init__.py (Added route for getdata)
views.py (implementation of getdata)
|
myclient
|
index.html (AJAX query)
Contents of myclient/index.html
<html>
<head>
<meta charset="utf-8">
<title>HOME UI</title>
</head>
<body>
<button id="submit">Give it now!</button>
<script src="./jquery-2.1.3.min.js"></script>
<script>$("#submit").on('click', function()
{
$.ajax(
{
type: "GET",
async: false,
url: "../myproject/data/getdata",
success: function (data)
{
console.log("LED On" );
},
error: function ()
{
console.error("ERROR");
}
});
});</script></body></html>
File myproject/__init__.py
from pyramid.config import Configurator
from pyramid.renderers import JSONP
import os
import logging
def includeme(config):
""" If include function exists, write this space.
"""
pass
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
config = Configurator(settings=settings)
config.add_renderer('jsonp', JSONP(param_name='callback'))
config.include(includeme)
directory = "/home/redmine/Downloads/MyProject/myproject/mydata/"
for root,dir,files in os.walk(directory):
if root == directory:# Walk will return all sublevels.
for dirs in dir: #This is a tuple so we need to parse it
config.include('myproject.mydata.' + str(dirs), route_prefix='/' + str(dirs))
config.add_static_view('static', 'prototype', cache_max_age=3600)
config.scan()
return config.make_wsgi_app()
File myproject/views.py
from pyramid.view import view_config
File myproject/mydata/__init__.py
import data
File mproject/mydata/data/__init__.py
from pyramid.config import Configurator
def includeme(config):
config.add_route('get_data', 'getdata', xhr=True)
def main(global_config, **settings):
print 'hello'
config = Configurator(settings=settings)
config.include(includeme, route_prefix='/data')
config.add_static_view('static', 'prototype', cache_max_age=3600)
config.scan('data')
return config.make_wsgi_app()
File mproject/mydata/data/views.py
from pyramid.view import view_config
import json
#view_config(route_name='get_data', xhr=True, renderer='jsonp')
def get_data(request):
return "{'firstName' : 'John'}"
File cherry_wsgi.py
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.paster import get_app
config = Configurator()
app = get_app('development.ini', 'main')
File cherry_server.py
from cherry_wsgi import app
import cherrypy
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': '/home/redmine/Downloads/MyProject/'
},
'/myclient': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './myclient'
}
}
if __name__ == '__main__':
cherrypy.tree.mount(app, "/", conf)
cherrypy.server.unsubscribe()
server = cherrypy._cpserver.Server()
server.socket_host = "0.0.0.0"
server.socket_port = 9090
server.thread_pool = 30
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
I'm not sure if I caught everything but I did see a couple of bugs. First off, your url was off in your ajax call. Next in your views.py you were using jsonp and not json as your renderer. Also, you were using "route_name" instead of "route" (ajax calls) for the #view_config. Finally, you were returning a string. I changed it to a dict.
Pyramid can be tricky if you don't set up your project structure in a straight forward way. I learned the hard way :)
Contents of myclient/index.html
<html>
<head>
<meta charset="utf-8">
<title>HOME UI</title>
</head>
<body>
<button id="submit">Give it now!</button>
<script src="./jquery-2.1.3.min.js"></script>
<script>$("#submit").on('click', function()
{
$.ajax(
{
type: "GET",
async: false,
url: "getdata",
success: function (data)
{
console.log("LED On" );
},
error: function ()
{
console.error("ERROR");
}
});
});
</script>
</body>
</html>
File mproject/mydata/data/views.py
from pyramid.view import view_config
#view_config(name='get_data', renderer='json')
def get_data(request):
return {'firstName' : 'John'}
Now after looking at you overall file structure it does not look like a standard pyramid app. You have a lot of things going and it looks like over programming to me. There is a lot of duplicate code. Maybe you are doing this for a reason but I don't know.
I included below a pyramid start git repo. I built it to help people get started putting there pyramid projects on Openshift. I would think your pyramid project should follow the same outline. No need for deep folders.
The file you want to pay particular close attention to is the "app.py.disabled" file. Don't mind the disabled part. There are two ways to start an Openshift pyramid app and this git repo is using the wsgi.py file. You can just switch the two.
Anyway, inside the app.py.disabled file you can see all the different ways that I have used to setup pyramid app using wsgi servers (simple, waitress, and cherrypy). Just uncomment/comment out the code you want.
I think you are mixing the cherrypy framework and pyramid framework. Just use cherrypy's wsgi server. Don't do any of the cherrypy configuration. The last I heard was that Cherrypy was separating out their wsgi server from their framework. It's been at least a year since I looked.
You might just try using Waitress. Very good and simple and works across all platforms.
Openshift-Pyramidstarter