I am trying to set up an API rest in Flask using inside osmnx and a function named nearest_node but doesn't work . The code I'm using is :
import networkx as nx
import osmnx as ox
from matplotlib import pyplot as plt
from flask import Flask
app = Flask(__name__)
G = ox.load_graphml('my.graphml')
orig_node = ox.distance.nearest_nodes(G,[40.670724], [16.597054])
dest_node = ox.distance.nearest_nodes(G,[40.669731], [16.606796])
#app.route('/')
def dijkstra():
route = nx.shortest_path(G,orig_node,dest_node,weight='length')
route_map = ox.plot_route_folium(G, route)
route_map.save('route.html')
return plt.show(route_map)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9090)
I haven't sintax error but on my homepage site I can't see nothing .
There is someone that can help me ?
Max
Related
I want to add app object once in main.py which can be used everywhere, but route does not work in this way. What is the issue here?
main.py
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
routes.py
from main import app
#app.route("/", methods = ["GET"])
def home():
return "hi"
However, if declare app = Flask(name) in routes.py and import app in main.py it is working all fine. Working scenario.
main.py
from routes import app
if __name__ == "__main__":
app.run(debug=True)
routes.py
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route("/", methods = ["GET"])
def home():
return "hi"
my objective is to define app in main.py and import it in other files, but getting issues.
main.py is not even aware that routes.py exists. Import your routes.py file after initializing your app.
# main.py
from flask import Flask
app = Flask(__name__)
import routes # needed
if __name__ == "__main__":
app.run(debug=True)
# routes.py
from __main__ import app
#app.route("/")
def home():
return "hi"
#market package
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from market import routes
app = Flask(__name__)
app.config\['SQLALCHEMY_DATABASE_URI'\] = 'sqlite:///market.db'
db = SQLAlchemy(app)
----------
from market import db #(models)
----------
from market import app #(routes)
from flask import render_template
from market.models import Item
----------#run
from market import app
if __name__ == '__main__':
app.run(debug=True)
Structuring the code into packages then importing modules but then getting the above error
I have created a Flask prediction app (within Google Colab) and when I am trying to run it post adding all the dependencies within the colab environment I am getting the url but when I click on it it show site cannot be reached.
I have the Procfile, the pickled model and the requirements text file but for some reason its not working. Also, I tried deploying this app using Heroku and it met the same fate where I got the app error.
For more context please visit my github repo.
Any help or guidance will be highly appreciated.
from flask import Flask, url_for, redirect, render_template, jsonify
from pycaret.classification import*
import pandas as pd
import numpy as np
import pickle
app = Flask(__name__)
model = load_model('Final RF Model 23JUL2021')
cols = ['AHT','NTT','Sentiment','Complaints','Repeats']
#app.route('/')
def home():
return render_template("home.html")
#app.route('/predict',methods=['POST'])
def predict():
int_features = [x for x in request.form.values()]
final = np.array(int_features)
data_unseen = pd.DataFrame([finak], columns = cols)
prediction = predict_model(model, data=data_unseen, round=0)
prediction = int(prediction.Label[0])
return render_template('home.html',pred='Predicted Maturiy Level is{}'.format(prediction))
#app.route('/predict_api',methods=['POST'])
def predict_api():
data = request.get_json(force=True)
data_unseen = pd.DataFrame([data])
prediction = predict_model(model, data=data_unseen)
output = prediction.Label[0]
return jsonify(output)
if __name__ == '__main__':
app.run(debug=True)
You cannot run flask app same as in your machine. You need to use flask-ngrok.
!pip install flask-ngrok
from flask_ngrok import run_with_ngrok
[...]
app = Flask(__name__)
run_with_ngrok(app)
[...]
app.run()
You can't use debug=True parameter in ngrok.
I am new to bokeh. I am struggling to get around error 404 when using AjaxDataSource . Following is the simple code which will update the plot every 2 seconds.
from flask import Flask, render_template, request, jsonify
from bokeh.embed import components
from bokeh.plotting import figure
from bokeh.models.sources import AjaxDataSource
app = Flask(__name__)
x = 0
#app.route('/data/', methods=['POST'])
def data():
global x
x += 1
y = 2 ** x
return jsonify(x=x, y=y)
#app.route("/dash")
def showChanges():
plots = []
plots.append(funcEmbedFig())
return render_template('extendTest.html', plots=plots)
def funcEmbedFig():
source = AjaxDataSource(data_url=request.url_root + 'data/',
polling_interval=2000, mode='append')
source.data = dict(x=[], y=[])
plot = figure(plot_height=300, sizing_mode='scale_width')
plot.line('x', 'y', source=source, line_width=4)
script, div = components(plot)
return script, div
if __name__ == '__main__':
app.run()
How can I get around this?
Any help would be much useful,
Thanks.
Have you tried flask’s url_for(“data”) as the data_url parameter?
I have the following code:
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
import pypyodbc
import pdb
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pypyodbc://SERVERNAME/DB'
db = SQLAlchemy(app)
#app.route('/')
def main():
result = db.engine.execute('SELECT * from Table')
return render_template('index.html')
This results in the following error:
ImportError: DLL load failed: The specified procedure could not be found.
I cannot find an example of this in the documentation...what is the correct way to use the SQLAlchemy object's engine?
Thanks!