deployment of Flask application to OpenShift - python

I would like to deploy my application which works fine on my local unix server to OpenShift cloud. I registered there and checkout git repository. But I don't know what to do now.
Application in this repository has following structure:
/libs
/app.py
/setup.py
/wsgi
static/
application
but I don't know where I should copy my project in which files should be modified. My project structure is following
/domain.wsgi
/domain/
app.py
infrastructure.py
models/
static/
templates/
views/
domain.wsgi
import sys, os
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
from domain.app import app as application
app.py
from infrastructure import app
import views.index
import views.login
import views.logout
import models.sa
infrastructure.py
from flask import Flask, g
from flask.ext.sqlalchemy import SQLAlchemy
from models.sa import get_user_class, UserQuery
from models.database import db_session
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://.............'
db = SQLAlchemy(app)
## Set SQL Alchemy to automatically tear down
#app.teardown_request
def shutdown_session(exception=None):
db_session.remove()
# Instantiate authentication
User = get_user_class(db.Model)
# config
app.config.update(
DEBUG = True,
SECRET_KEY = 'xxxxxxxxx'
)
Thanks

You files go under wsgi folder; please see this tutorial on how to deploy flask applications to OpenShift: http://www.paasmag.com/2013/01/08/beginners-guide-to-writing-flask-apps-on-openshift/

Related

Run flask app with python instead of flask run

I'm trying to run my flask app with "python app.py" instead of "flask run" command.
My goal is to launch the app on cpanel server and just about every tutorial requires the applications to be called using the "python" method.
Here is my folder structure:
project
webapp
init.py
templates
static
auth.py
main.py
app.py <-------------- I want this to be called with python instead of flask run command outside the folder
Here is my init_.py file:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = '9OLWxND4o83j4iuopO'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
#login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
And app.py is:
from webapp import app
I'm a newbie in flask, any help is appreciated
Insert the call to create_app at the end of init.py:
if __name__ == '__main__':
create_app().run(host='0.0.0.0', port=5000, debug=True)
The if statement avoid calling the app many times. It can only be called directly. Flask default host is 127.0.0.1 (localhost). Use 0.0.0.0 at production for better traffic monitoring. Default port is also 5000, so it's up to you to include. For better readability, you should explicit it.
Then call it
python webapp/init.py

Python flask with multi modules application OperationalError: (sqlite3.OperationalError) no such table

I have been learning python recently and practiced a couple of tutorials and its been going fine, till I have divided my application to sub modules to make it easier to maintain using blueprints, after doing so the application crashed and throw the following error: OperationalError: (sqlite3.OperationalError) no such table, here is how im dividing my application:
-instance
-flask.cfg
-app
- static // Folder for css
- templates // Folder for common html files
-__init__.py // to init the app
-module1
-__init__.py // to register module blueprint
-subModule1
-subModule1 Group1
-templates
-htmls files
-__init__.py file //to register group1 blueprint
-Group1Form
-Group1Model
-Group1View
-subModule1 Group2
-templates
-htmls files
-__init__.py file //to register group2 blueprint
-Group2Form
-Group2Model
-Group2View
-subModule2
....
-main.py
The application run fine when I use flask run but I have an issue when I use db.session.add() method with the exception that the table was not found. I have tried many solutions to this issue but no luck. here is some links for the same issue:
OperationalError: (sqlite3.OperationalError) no such table: user
https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/
Python sqlite3.OperationalError: no such table:
'No application found. Either work inside a view function or push an application context.'
Here is the flask.cfg file:
import os
BASEDIR = os.path.abspath(os.path.dirname(__file__))
SECRET_KEY = os.urandom(32)
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.abspath('app.db')
And here is how I am initializing the app in the main __init__.py file:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app(config_filename=None):
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile(config_filename)
initialize_extensions(app)
register_blueprints(app)
def create_app(config_filename=None):
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile(config_filename)
initialize_extensions(app)
register_blueprints(app)
return app
def initialize_extensions(app):
db.init_app(app)
with app.app_context():
db.create_all()
def register_blueprints(app):
from app.module1.config.university import university_blueprint
app.register_blueprint(university_blueprint)
from app.module1.config.department import department_blueprint
app.register_blueprint(department_blueprint)
from app.module1.authors.author import author_blueprint
app.register_blueprint(author_blueprint)
from app.module1.authors.publications import publications_blueprint
app.register_blueprint(publications_blueprint)
can you move db.init_app() inside app.app_context() function
with app.app_context():
db.init_app()
I figured out the issue, I was suppose to place:
with app.app_context():
db.create_all()
after registering the blueprints

URL not found error when deploying flask app using CGI

I'm trying to deploy a flask app on a shared server (hostgator) using CGI but I'm getting a URL not found error when I use the base URL (mysite.com/) and all others. I've set up a virtual environment in the public_html/ folder with python 2.7.8 and Flask 0.12.4 (newer versions of flask not supported on python 2.7.8 because they use SSLContext).
I've determined that the .htaccess file and cgi-bin/main.cgi file are configured correctly by writing a small flask hello_world app in the public_html/ folder that works and runs the website. And I've tried checking the error logs on cpanel which are empty and couldn't find any other useful logs in the file system. I've also set all the permissions to 755 which is what hostgator recommended to no avail.
My file system is this:
public_html/
flask_hello_world.py
some .html files for testing
cgi-bin/
main.cgi
server/
app.py
__init__.py
views.py
models.py (sqlalchemy models)
static/
some .html files
My .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home4/MYSITE/public_html/cgi-bin/main.cgi/$1 [L]
Addhandler cgi-script .py .cgi
main.cgi:
#!/home4/MYSITE/public_html/venv/bin/python
import sys
sys.path.insert(0, '/home4/MYSITE/public_html/venv/lib/python2.7/site-packages/')
sys.path.insert(1, '/home4/MYSITE/public_html/server/')
from wsgiref.handlers import CGIHandler
from app import app
class ProxyFix(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SERVER_NAME'] = ""
environ['SERVER_PORT'] = "80"
environ['REQUEST_METHOD'] = "GET"
environ['SCRIPT_NAME'] = ""
environ['QUERY_STRING'] = ""
environ['SERVER_PROTOCOL'] = "HTTP/1.1"
return self.app(environ, start_response)
if __name__ == '__main__':
app.wsgi_app = ProxyFix(app.wsgi_app)
CGIHandler().run(app)
And when I changed main.cgi to run flask_hello_world.py it ran just fine. Here's that code:
import flask
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return flask.send_from_directory('/home4/MYSITE/public_html/','index.html')
#app.route('/hello')
def hello():
return flask.send_from_directory('/home4/MYSITE/public_html/','hello.html')
Moving my main project .py files into the public_html folder did not solve the problem. Heres app.py and init.py from the actual project:
app.py:
import os
basedir = os.path.abspath(os.path.dirname(__file__))
from flask import Flask
##other imports including sqlalchemy
app = Flask(__name__)
app.config["CACHE_TYPE"] = "null"
# sqlalchemy setup
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database.db')
##some other configurations
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# flask login setup
login_manager = LoginManager()
login_manager.login_view = "login_view"
login_manager.init_app(app)
init.py:
from app import app, db, login_manager
import models
import views
My views.py has no import errors when I just run it using 'python views.py' and the route for '/' is identical to the hello world app. So I think there is a problem with app.py, init.py, or the fact that my routes aren't in the app.py file.
The problem might have been that the views are not imported in app.py or init.py, so are unknown to the application. Thus the CGI script threw the URL not found.

Flask-PyMongo with application factory and blueprints

I am trying to implement Flask-PyMongo with blueprints and an application factory and keep getting AttributeError: 'Flask' object has no attribute 'db'
My directory structure looks like
myapp/
myapp.py
config.py
/app
__init__.py
/v1
__init__.py
endpoints.py
In my python script that starts the Flask app I have:
import os
from app import create_app
app = create_app('dev')
In my top level init.py I have:
mongo = PyMongo()
def create_app(config_name):
app = Flask(__name__)
mongo.init_app(app)
app.config.from_object(config[config_name])
from app.v1 import psapi as psapi_bp
app.register_blueprint(psapi_bp, url_prefix='/api')
if not os.path.exists('logs'):
os.mkdir('logs')
In my endpoints.py I have a route that looks like
#myapp.route('/addentry', methods=['POST'])
def addentry():
username = request.json['username']
userid = current_app.db.user_entry.insert({'username':username})
return jsonify({'userid':userid})
I feel like there is something small that I am missing but I am not seeing it.
You need to call db on your mongo object, not on the app object
to those who may be facing this problem again :
you should first define mongo oustside create_app to have access to it from inside other files.
then init_app with that like the following:
from flask import Flask, current_app
from flask_pymongo import PyMongo
mongo = PyMongo()
def create_app(config_name):
app = Flask(__name__, instance_relative_config=False)
app.config.from_object(app_config[config_name])
# INIT EXTENSIONS ----------------------
mongo.init_app(app)
return app
then in any file you can import mongo from above file. for example:
from ../factory import mongo

How to structure Flask User app?

I use supervisor to run my app. It is structured as follows:
My app layout
my_app
__init__.py
my_app
__init__.py
startup
create_app.py
create_users.py
common_settings.py
core
__init__.py
models.py
views.py
Outer __init__.py
from my_app import app
Inner __init__.py
from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__) # The WSGI compliant web application object
db = SQLAlchemy(app) # Setup Flask-SQLAlchemy
manager = Manager(app) # Setup Flask-Script
from my_app.startup.create_app import create_app
create_app()
create_app.py
def create_app(extra_config_settings={}):
"""
Initialize Flask applicaton
"""
# ***** Initialize app config settings *****
# Read common settings from 'app/startup/common_settings.py' file
app.config.from_object('app.startup.common_settings')
# Read environment-specific settings from file defined by OS environment variable 'ENV_SETTINGS_FILE'
app.config.from_envvar('ENV_SETTINGS_FILE')
# Load all blueprints with their manager commands, models and views
# Setup Flask-User to handle user account related forms
from my_app.core.models import User
# Setup Flask-User
db_adapter = SQLAlchemyAdapter(db, User) # Setup the SQLAlchemy DB Adapter
user_manager = UserManager(db_adapter, app) # Init Flask-User and bind to app
from my_app import core
return app
my_app/core/__init__.py
from . import models
from . import views
views.py
from my_app import db, app
'''
Register a new user
'''
#app.route('/register', methods = ['POST'])
def register_user():
user_manager = app.user_manager
db_adapter = user_manager.db_adapter
I was trying to follow an example I found online.
I'm creating the variables db_adapter and user_manager in create_app(). Are these the same ones being used in my views.py?
If anyone has any suggestions or links to examples that I can follow to structure my project, it would be greatly appreciated.
Thanks.
Assuming that that's how Flask-User works (sets the user_manager attribute on app), this is trivial to determine, just compare them in the create_app function when you still have a direct reference to the objects.
db_adapter = SQLAlchemyAdapter(db, User)
user_manager = UserManager(db_adapter, app)
assert db_adapter is user_manager.db_adapter
assert user_manager is app.user_manager
However, your entire project layout doesn't make much sense. You should be creating the entire app inside the create_app factory. You should not have an __init__.py file at the top level, that's the project folder not the package. You should use current_app within views to access the app, since it will only be created at runtime by the factory. You should create a manage.py file at the project level to use the factory.
my_project/
my_app/
__init__.py
models.py
views.py
defaults.py
instance/
config.py
manage.py
__init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('my_app.defaults')
app.config.from_pyfile('config.py')
db.init_app(app)
from my_app.views import bp
app.register_blueprint(bp)
return app
models.py:
from my_app import db
class User(db.Model):
...
views.py:
from flask import Blueprint, render_template
from my_app.models import User
bp = Blueprint('app', __name__)
#bp.route('/')
def index():
return render_template('index.html')
manage.py:
#!/usr/bin/env python
from flask_script import Manager
from my_app import create_app
Manager(create_app).run()

Categories