I am trying to re-factor and re-structure my routes into views/routes.py, but all these routes are using one common open cassandra connection. open means that the cassandra connection will be connected when starting server and will stay open during the server running. The connected connection will be used by all routes. I am thinking about blueprints. But how to re-factor and re-structure my codes?
app = Flask(__name__)
dbConnection = None
#app.route('/api/v1/<para1>', methods=['GET', 'OPTIONS'])
#crossdomain(origin="*")
def funnel(para1):
# codes using cassandra Connection
#app.route('/api/v1/xxx/<para2>', methods=['GET', 'OPTIONS'])
#crossdomain(origin="*")
def funnel(para2):
# codes using cassandra Connection
#app.route('/api/v1/yyy/<para3>', methods=['GET', 'OPTIONS'])
#crossdomain(origin="*")
def funnel(para3):
# codes using cassandra Connection
if __name__ == '__main__':
# Connect db connection
app.run(host='0.0.0.0', port=8080, debug=True)
The following codes seem work. But any comments welcomed.
myblueprint.py
from flask import Blueprint
class MyBlueprint(Blueprint):
dbconnection = None
def setDb( self, db ):
self.dbconnection = db
myview.py
my_api = MyBlueprint('my_api', __name__ )
#my_api.route('/api/v1/yyy/<para3>', methods=['GET', 'OPTIONS'])
#crossdomain(origin="*")
def myapi(para3):
# codes using funnel_api.dbconnection
app.py
app = Flask(__name__)
cassandraConnection = None
if __name__ == '__main__':
my_api.setDb( cassandraConnection )
app.register_blueprint( my_api )
#SETUP RESOURCE AND START SERVER
app.run(host='0.0.0.0', port=5000, debug=True)
Related
I wanted to host my flask app on a specific port but the method I am using is not working. What I did is assign the host and port properties in my socket.run(). When I go to the specified address the page doesn't load. Where did I go wrong and how can I properly host a flask app with specific ip address and port. Thanks in advance.
EDIT: when I run the app with python app.py it works but when I run it with flask run it doesn't work.
from flask import Flask, render_template, Response
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'blahBlah'
socket = SocketIO(app)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
socket.run(app, host='127.0.0.1', port=7000)
As of Flask version 0.11, setting the port param will not take affect unless config variable SERVER_NAME is set and debug=True.
Your regular Flask app will be running on default (localhost:5000).
Therefore the code should look like:
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'blahBlah'
app.config['SERVER_NAME'] = '127.0.0.1:8000'
socket = SocketIO(app)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
socket.run(app, debug=True)
See Flask ref for more information: flask API
Edit:
Above code example will work so forth your code is structured:
project
app.py
templates
index.html
To run the code say:
python app.py
Running the code with the flask tools (flask run) will run the app and not the SocketIO part.
The right way
The right way to run the code is to do like this:
from flask import Flask, render_template
from flask_socketio import SocketIO
def create_app():
app = Flask(__name__)
app.config.from_mapping(
SECRET_KEY='BlaBla'
)
socket = SocketIO(app)
#app.route('/')
def index():
return render_template('index.html')
return app
Then in the shell run:
export FLASK_RUN_PORT=8000
Now you can run the flask app with the flask command:
flask --app app --debug run
maybe u are hosting something else on that specific port or on that specific ip or try to replace the socket.run by app.run
I am building a API that uses flask. I am going to use uWSGI and NGINX in production. The API is working as it is, but I am trying to rebuild it to use classes. When I run it after I have build it as a class it gives me Internal Server Error.
The following files are working:
src/server.py
from flask import Flask
app = Flask(__name__)
#app.route("/ping")
def ping():
return "pong"
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
src/wsgi.py
from src.server import app
if __name__ == "__main__":
app.run()
When I try to build it as class then I get Internal Server Error
src/server.py
from flask import Flask
class Main:
app = Flask(__name__)
def __init__(self):
#self.app.route("/ping")
def ping():
return "pong"
if __name__ == "__main__":
main = Main()
main.app.run(host="0.0.0.0", debug=True)
src/wsgi.py
from src.server import Main
if __name__ == "__main__":
main = Main()
main.app.run()
# project\__init__.py
from flask import Flask
from flask_mysqldb import MySQL
from .config import app_config
db = MySQL()
def create_app(config_name):
app = Flask(__name__,
instance_path=os.path.join(os.path.dirname(__file__), 'instance'),
instance_relative_config=True)
app.config.from_object(app_config[config_name])
db.init_app(app)
print(db) # prints <flask_mysqldb.MySQL object at 0x000002A13710FC10>
# project/main.py
from . import db
#main.route('/foobar')
def foobar():
print(db) # prints <flask_mysqldb.MySQL object at 0x000002A13710FC10>
# project/database/seed_shipment.py
from project import create_app, db
def foo():
.
.
.
def goo()
.
.
.
if __name__ == '__main__':
config_name = os.getenv('FLASK_ENV')
app = create_app(config_name)
cursor = db.connection.cursor()
print(db) # prints <flask_mysqldb.MySQL object at 0x000002056B4EFD60>
print(db.connection) # returns None
for x in range(20):
# code which generates dummy data using foo() and goo()
cursor.execute("INSERT INTO shipment (column1, column2) VALUES (%s)", (var1, var2))
db.connection.commit()
My database connection works fine when I host the app and carry out CRUD operations using the interface in my browser. Such as login, sign up, create a shipment.
Note that I am not executing flask run from my terminal but instead python -m project.run, here is the code of this script:
# project/run.py
import os
from . import create_app
config_name = os.getenv('FLASK_ENV')
app = create_app(config_name)
if __name__ == '__main__':
app.run()
However, when I run python -m project.database.seed_shipment db seems to reference a different MySQL instance which has no connection. See the print results in the comments in my code.
My database connection works fine when I host the app and carry out CRUD operations using the interface in my browser. Such as login, sign up, create a shipment.
This indicates to me that you are correctly connecting to your db for each request.
However, when I run python -m project.database.seed_shipment db seems to reference a different MySQL instance which has no connection. See the print results in the comments in my code.
This, on the other hand, indicates to me that you are not connecting to your db when you directly execute seed_shipment - in fact, you seem to reference something different (as you write). In other words, db = MySQL() is not called.
I recommend that you try the following.
# project\__init__.py
from flask import Flask, g
from flask_mysqldb import MySQL
from .config import app_config
def create_app(config_name):
app = Flask(__name__,
instance_path=os.path.join(os.path.dirname(__file__), 'instance'),
instance_relative_config=True)
app.config.from_object(app_config[config_name])
# this here is just to register the teardown context and some variables ....
MySQL(app)
return app
def get_db():
if 'db' not in g:
g.db = MySQL().connection.cursor()
return g.db
That way, MySQL(app) is always executed when you call create_app. Now, you should be able to simply call get_db each time you want your db.
after a working production app was connected to local mongo, we've decided to move to Mongo Atlas.
This caused production errors.
Our stack is docker -> alping 3.6 -> python 2.7.13 -> Flask -> uwsgi (2.0.17) -> nginx running on aws
flask-mongoengine-0.9.3 mongoengine-0.14.3 pymongo-3.5.1
when starting the app in staging/production, The uwsgi is sending No replica set members found yet.
We don't know why.
We've tried different connection settings connect: False which is lazy connecting, meaning not on initializing, but on first query.
It caused the nginx to fail for resource temporarily unavailable error on some of our apps. We had to restart multiple times for the app to finally start serving requests.
I think the issue is with pymongo and the fact that it's not fork-safe
http://api.mongodb.com/python/current/faq.html?highlight=thread#id3
and uwsgi is using forks
I suspect it might be related to the way my app is being initalized.
might by aginst Using PyMongo with Multiprocessing
here is the app init code:
from app import FlaskApp
from flask import current_app
app = None
from flask_mongoengine import MongoEngine
import logging
application, app = init_flask_app(app_instance, module_name='my_module')
def init_flask_app(app_instance, **kwargs):
app_instance.init_instance(environment_config)
application = app_instance.get_instance()
app = application.app
return application, app
# app_instance.py
import FlaskApp
def init_instance(env):
global app
app = FlaskApp(env)
return app
def get_instance():
if globals().get('app') is None:
app = current_app.flask_app_object
else:
app = globals().get('app')
assert app is not None
return app
class FlaskApp(object):
def __init__(self, env):
.....
# Initialize the DB
self.db = Database(self.app)
....
# used in app_instance.py to get the flask app object in case it's None
self.app.flask_app_object = self
def run_server(self):
self.app.run(host=self.app.config['HOST'], port=self.app.config['PORT'], debug=self.app.config['DEBUG'])
class Database(object):
def __init__(self, app):
self.db = MongoEngine(app)
def drop_all(self, database_name):
logging.warn("Dropping database %s" % database_name)
self.db.connection.drop_database(database_name)
if __name__ == '__main__':
application.run_server()
help in debugging this will be appreciated!
I'm using web.py for an app and I'd like to use livereload while being in development but I can't figure out how to make the two get along?
app.py
import web
from livereload import Server
urls = (
'/', 'index'
)
class index:
def GET(self):
return "Hello, Kitty"
if __name__ == '__main__':
app = web.application(urls, globals()).wsgifunc()
server = Server(app)
server.serve()
I'm following https://github.com/lepture/python-livereload
server = Server(app.wsgi_app)
server.serve()
in my terminal I type
livereload app.py
and I see
[I 161130 14:05:03 server:283] Serving on http://127.0.0.1:35729
[I 161130 14:05:03 handlers:60] Start watching changes
[I 161130 14:05:03 handlers:62] Start detecting changes
but in the browser when loading http://127.0.0.1:35729 I see this
import web
from livereload import Server
urls = (
'/', 'index'
)
class index:
def GET(self):
return "Hello, Pussy"
if __name__ == '__main__':
app = web.application(urls, globals()).wsgifunc()
server = Server(app)
server.serve()
I see the python script rendered as plain tex.
Update
I decided to give the whole thing a second try, and succeeded, good for me :)
from livereload import Server
import web
from nestpas.views import *
from nestpas.urls import *
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# web.config.debug = False
web.ctx.debug = False
app = web.application(urls, globals(), autoreload=False)
webapp = app.wsgifunc()
if __name__ == '__main__':
## app.run()
server = Server(webapp)
server.watch('static/', 'templates/', 'nestpas/')
server.serve(port=8080, host='localhost')
The files I'm using are stored in static, templates and nestpas which is where the Python modules are stored. I ran into some issues having sessions stored on disk, but I managed to change that as well by using the DBStore instead
from livereload import Server
import web
from nestpas.views import *
from nestpas.urls import *
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
# web.config.debug = False
web.ctx.debug = False
app = web.application(urls, globals(), autoreload=False)
webapp = app.wsgifunc()
# Setup session storage
db = web.database(dbn='sqlite', db='dev.db')
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store)
if __name__ == '__main__':
## app.run()
server = Server(webapp)
server.watch('static/', 'templates/', 'nestpas/')
server.serve(port=8080, host='localhost')
The next goal is to figure out how to use Livereload only when on localhost and not have to think about it when deploying to production.