def add_flask_profiler(app, pg_settings=settings["POSTGRES"]): try: if settings.PROFILER["flask-profiler"]: from flask_profiler import Profiler app.config["flask_profiler"] = { "enabled": settings.DEBUG, "storage": { "engine": "sqlalchemy", "db_url": pg_settings["uri"] + "/" + pg_settings["db"], }, "basicAuth": { "enabled": True, "username": "******", "password": "******", }, "ignore": ["^/static/.*"], } log.info( f"flask_profiler starting at url : http://{settings.API.SERVER.url}:{settings.API.SERVER.port}/flask-profiler/" ) log.info( f"flask_profiler starting with storage setup {app.config['flask_profiler']['storage']}" ) Profiler(app) except ImportError: log.info("flask_profiler ImportError")
def add_flask_profiler(flask_app): # https://github.com/muatik/flask-profiler from flask_profiler import Profiler # You need to declare necessary configuration to initialize # flask-profiler as follows: flask_app.config["flask_profiler"] = { "enabled": True, "storage": { "engine": "sqlite" }, "basicAuth": { "enabled": True, "username": "******", "password": "******" }, "ignore": ["^/static/.*"] } profiler = Profiler() profiler.init_app(flask_app)
import os from flask import Flask from flask_profiler import Profiler from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow from app.config import TestingConfig, DevelopmentConfig, ProductionConfig app = Flask(__name__) app.config.from_object(DevelopmentConfig) db = SQLAlchemy() profiler = Profiler() ma = Marshmallow() from app.cve.routes import cve from app.cpe.routes import cpe from app.software.routes import software from app.errors.handlers import errors from app.docs.routes import swag, swaggerui_blueprint, SWAGGER_URL app.register_blueprint(cve, url_prefix="/api/cve") app.register_blueprint(cpe, url_prefix="/api/cpe") app.register_blueprint(software, url_prefix="/api/software") app.register_blueprint(errors) app.register_blueprint(swag) app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL) def create_app(): app = Flask(__name__) app.config.from_object(DevelopmentConfig if str(
_HERE = os.path.dirname(__file__) _SETTINGS = os.path.join(_HERE, 'settings.ini') app = create_app(blueprints=blueprints, settings=_SETTINGS) #app = Flask(__name__) app.config["DEBUG"] = True # You need to declare necessary configuration to initialize # flask-profiler as follows: app.config["flask_profiler"] = { "enabled": app.config["DEBUG"], "storage": { "engine": "sqlite" }, "basicAuth":{ "enabled": True, "username": "******", "password": "******" }, "ignore": [ "^/static/.*" ] } profiler = Profiler(app) if __name__ == '__main__': app.run(host="127.0.0.1", port=5000)
from flask import Flask from flask import render_template from flask_profiler import Profiler profile = Profiler() app = Flask(__name__, template_folder="data") app.config["DEBUG"] = True app.config["flask_profiler"] = { "enabled": app.config["DEBUG"], "storage": { "engine": "sqlite" }, "basicAuth": { "enabled": False, "login": "******", "password": "******" }, "ignore": [ "^/static/.*", ], "endpointRoot": "metrics" } @app.route('/') def index(): return render_template('index.html')
def create_app(): # get current configuration, or default to 'production' for safety config_name = os.environ.get('FLASK_ENV') or 'production' # load configuration files from 'instance' folder instance_dir = (Path(__file__).parent / "instance").absolute() app = Flask(__name__, instance_relative_config=True, instance_path=str(instance_dir)) app.config.from_object(app_config[config_name]) app.config.from_pyfile('secrets.py') app.config.from_pyfile('mail.py') app.config.from_pyfile('rollbar.py') # app.config.from_pyfile('scout.py') app.config.from_pyfile('local.py') # create a long-lived Redis connection app.config['REDIS_SESSION'] = redis.Redis.from_url(url=app.config['CACHE_REDIS_URL']) # create long-lived Mongo connection for Flask-Sessionstore app.config['SESSION_MONGODB'] = MongoClient(host=app.config['SESSION_MONGO_URL']) # we have two proxies -- we're behind both waitress and nginx app.wsgi_app = ProxyFix(app.wsgi_app, x_for=2) if app.config.get('PROFILE_MEMORY', False): app.wsgi_app = Dozer(app.wsgi_app) db.init_app(app) migrate = Migrate(app, db) bootstrap = Bootstrap(app) mail = Mail(app) bleach = Bleach(app) md = Markdown(app, extensions=[makeExtension(configs={'entities': 'named'})]) rb = Rollbar(app) qr = QRcode(app) bbl = Babel(app) session_store = Session(app) cache.init_app(app) # add endpoint profiler and rate limiter in production mode # also add handler to direct Waitress logging output to the console if config_name == 'production': profiler = Profiler(app) # set up Flask-Limiter limiter.init_app(app) # add debug toolbar if in debug mode if config_name == 'development': toolbar = DebugToolbarExtension(app) # api_toolbar = DebugAPIExtension(app) # panels = list(app.config['DEBUG_TB_PANELS']) # panels.append('flask_debug_api.BrowseAPIPanel') # panels.append('flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel') # app.config['DEBUG_TB_PANELS'] = panels # set up CSS and javascript assets env = Environment(app) if not app.debug: from logging import INFO, Formatter, basicConfig from logging.handlers import RotatingFileHandler basicConfig(level=INFO) file_handler = RotatingFileHandler(app.config['LOG_FILE'], 'a', 1 * 1024 * 1024, 10) file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) app.logger.setLevel(INFO) file_handler.setLevel(INFO) app.logger.addHandler(file_handler) app.logger.info('MPS Project Manager starting') # use Werkzeug built-in profiler if profile-to-disk is enabled if app.config.get('PROFILE_TO_DISK', False): app.config['PROFILE'] = True app.wsgi_app = ProfilerMiddleware(app.wsgi_app, profile_dir=app.config.get('PROFILE_DIRECTORY')) app.logger.info('Profiling to disk enabled') # configure behaviour for uploaded files asset_folder = Path(app.config.get('ASSETS_FOLDER')) uploaded_subfolder = Path(app.config.get('ASSETS_UPLOADED_SUBFOLDER')) submitted_subfolder = Path(app.config.get('ASSETS_SUBMITTED_SUBFOLDER')) abs_uploaded_path = asset_folder / uploaded_subfolder abs_uploaded_path.mkdir(parents=True, exist_ok=True) abs_submissions_path = asset_folder / submitted_subfolder abs_submissions_path.mkdir(parents=True, exist_ok=True) app.config['UPLOADED_SOLUTIONS_DEST'] = abs_uploaded_path app.config['UPLOADED_BATCHUSERLIST_DEST'] = abs_uploaded_path app.config['UPLOADED_SUBMISSIONS_DEST'] = abs_submissions_path configure_uploads(app, [solution_files, batch_user_files, submitted_files]) # configure Flask-Security, which needs access to the database models for User and Role from app import models user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role) # patch Flask-Security's login form to include some descriptive text on the email field security = Security(app, user_datastore, login_form=PatchedLoginForm, mail_util_cls=PatchedMailUtil) if config_name == 'production': # set up more stringent limits for login view and forgot-password view # add to a particular view function. login = app.view_functions['security.login'] forgot = app.view_functions['security.forgot_password'] limiter.limit("50/day;5/minute")(login) limiter.limit("50/day;5/minute")(forgot) # set up celery and store in extensions dictionary celery = make_celery(app) app.extensions['celery'] = celery # register celery tasks # there doesn't seem a good way of doing this using factory functions! Here I compromise by passing the # celery application instance to a collection of register_*() functions, which use an @celery decorator # to register callables. Then we write the callable into the app, in the 'tasks' dictionary app.tasks = {} tasks.register_send_log_email(celery, mail) tasks.register_utility_tasks(celery) tasks.register_prune_email(celery) tasks.register_backup_tasks(celery) tasks.register_rollover_tasks(celery) tasks.register_issue_confirm_tasks(celery) tasks.register_golive_tasks(celery) tasks.register_close_selection_tasks(celery) tasks.register_user_launch_tasks(celery) tasks.register_popularity_tasks(celery) tasks.register_matching_tasks(celery) tasks.register_matching_email_tasks(celery) tasks.register_availability_tasks(celery) tasks.register_scheduling_tasks(celery) tasks.register_maintenance_tasks(celery) tasks.register_assessment_tasks(celery) tasks.register_assessor_tasks(celery) tasks.register_email_notification_tasks(celery) tasks.register_precompute_tasks(celery) tasks.register_push_feedback_tasks(celery) tasks.register_system_tasks(celery) tasks.register_batch_create_tasks(celery) tasks.register_selecting_tasks(celery) tasks.register_session_tasks(celery) tasks.register_marking_tasks(celery) tasks.register_services_tasks(celery) tasks.register_test_tasks(celery) @security.login_context_processor def login_context_processor(): # build list of system messages to consider displaying on login screen messages = [] for message in MessageOfTheDay.query.filter_by(show_login=True).all(): if message.project_classes.first() is None: messages.append(message) return dict(messages=messages) @app.before_request def before_request_handler(): if current_user.is_authenticated: if request.endpoint is not None and 'ajax' not in request.endpoint: try: Notification.query.filter_by(remove_on_pageload=True).delete() db.session.commit() except SQLAlchemyError as e: current_app.logger.exception("SQLAlchemyError exception", exc_info=e) @app.template_filter('dealingwithdollars') def dealingwithdollars(latex_string): if latex_string is None: return r'<div class="alert alert-danger">An empty string was supplied. ' \ r'Please check your project description.</div>' splat = list(latex_string) # Splits string into list of characters dollar_inds = [i for i in range(0, len(splat)) if splat[i] == "$"] # Finds indices of all dollar signs display_inds = [] # Less pythonic than list comprehension, but now inline_inds can exclude double dollar signs for elem in dollar_inds: if elem != len(splat) - 1: if splat[elem + 1] == r"$": display_inds.append(elem) display_inds.append(elem + 1) inline_inds = [elem for elem in dollar_inds if splat[elem - 1] != "\\" and elem not in display_inds] # \$ is allowed in LaTeX, $ is not. just_dollar = [elem for elem in dollar_inds if elem not in inline_inds and elem not in display_inds] if len(inline_inds) % 2 != 0: # Checks for lonely dollar signs latex_string = r'<div class="alert alert-danger">Failed to match LaTeX dollar delimiters. ' \ r'Please check the markup in your project description.</div>' + latex_string else: # Only converts inline math delimiters, as latex2markdown seems to convert display math delimiters for i in range(0, len(inline_inds)): if i % 2 == 0: splat[inline_inds[i]] = r"\\(" else: splat[inline_inds[i]] = r"\\)" for elem in just_dollar: splat.pop(elem - 1) latex_string = ''.join(splat) l2m_obj = latex2markdown.LaTeX2Markdown(latex_string) mathjax_string = l2m_obj.to_markdown() return mathjax_string @app.template_filter('urlencode') def urlencode_filter(s): if s is None: return None s = s.encode('utf8') s = parse.quote_plus(s) return bleach.clean(s) def _get_previous_login(): if not has_request_context(): return None if session.get('previous_login', None) is not None: real_id = session['previous_login'] real_user = db.session.query(User).filter_by(id=real_id).first() else: real_user = None return real_user def _get_live_platform(): if not has_request_context(): return None return current_app.config.get('EMAIL_IS_LIVE', False) # collect all global context functions together in an attempt to avoid function call overhead @app.context_processor def global_context(): if not has_request_context(): return {} return {'get_previous_login': _get_previous_login, 'website_revision': site_revision, 'website_copyright_dates': site_copyright_dates, 'build_version': git_tag, 'home_dashboard_url': home_dashboard_url(), 'get_base_context': get_global_context_data, 'get_live_platform': _get_live_platform} @app.errorhandler(404) def not_found_error(error): return render_template('errors/404.html'), 404 @app.errorhandler(429) def rate_limit_error(error): return render_template('errors/429.html'), 429 @app.errorhandler(500) def internal_error(error): db.session.rollback() return render_template('errors/500.html'), 500 if not app.debug: @app.after_request def after_request(response): timeout = app.config['DATABASE_QUERY_TIMEOUT'] for query in get_debug_queries(): if query.duration >= timeout: app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % ( query.statement, query.parameters, query.duration, query.context)) return response @user_logged_in.connect_via(app) def login_callback(self, user): # DS 3 Feb 2019 - why did we want to clear notifications? # disabled this for now # # clear notifications for the user who has just logged in # Notification.query.filter_by(user_id=user.id).delete() # force precompute of expensive views celery = current_app.extensions['celery'] precompute_at_login(user, celery, autocommit=False) user.last_active = datetime.now() db.session.commit() from flask import Request class CustomRequest(Request): @property def rollbar_person(self): db.session.rollback() if current_user is None: return None # 'id' is required, 'username' and 'email' are indexed but optional. # all values are strings. return {'id': str(current_user.id), 'username': str(current_user.username), 'email': str(current_user.email)} app.request_class = CustomRequest # IMPORT BLUEPRINTS from .home import home as home_blueprint app.register_blueprint(home_blueprint, url_prefix='/') from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') from .admin import admin as admin_blueprint app.register_blueprint(admin_blueprint, url_prefix='/admin') from .faculty import faculty as faculty_blueprint app.register_blueprint(faculty_blueprint, url_prefix='/faculty') from .convenor import convenor as convenor_blueprint app.register_blueprint(convenor_blueprint, url_prefix='/convenor') from .student import student as student_blueprint app.register_blueprint(student_blueprint, url_prefix='/student') from .office import office as office_blueprint app.register_blueprint(office_blueprint, url_prefix='/office') from .reports import reports as reports_blueprint app.register_blueprint(reports_blueprint, url_prefix='/reports') from .user_approver import user_approver as user_approver_blueprint app.register_blueprint(user_approver_blueprint, url_prefix='/user_approver') from .project_approver import project_approver as project_approver_blueprint app.register_blueprint(project_approver_blueprint, url_prefix='/project_approver') from .loadbalancer import alb as alb_blueprint app.register_blueprint(alb_blueprint, url_prefix='/alb') from .manage_users import manage_users as manage_users_blueprint app.register_blueprint(manage_users_blueprint, url_prefix='/manage_users') from .documents import documents as documents_blueprint app.register_blueprint(documents_blueprint, url_prefix='/documents') from .services import services as services_blueprint app.register_blueprint(services_blueprint, url_prefix='/services') from .projecthub import projecthub as projecthub_blueprint app.register_blueprint(projecthub_blueprint, url_prefix='/projecthub') return app, celery
from flask import Flask from flask_profiler import Profiler profiler = Profiler() app = Flask(__name__) app.config['DEBUG'] = True app.config['flask_profiler'] = { "enabled" : app.config['DEBUG'], "storage" : { "engine" : "sqlite" }, "basicAuth" : { "enabled" : True, "username" : "admin", "password" : "admin" } } profiler.init_app(app) @app.route("/") def index(): return "Welcome to profiling!" @app.route("/user/<name>") def users(name): return f"The specified user is: {name}"
import os from flakon import create_app from hw1service.views import blueprints from flask_profiler import Profiler _HERE = os.path.dirname(__file__) _SETTINGS = os.path.join(_HERE, 'settings.ini') app = create_app(blueprints=blueprints, settings=_SETTINGS) app.config["DEBUG"] = True app.config["flask_profiler"] = { "enabled": app.config["DEBUG"], "storage": { "engine": "sqlite" }, "basicAuth": { "enabled": True, "username": "******", "password": "******" }, "ignore": ["^/static/.*"] } profiler = Profiler(app) # You can have this in another module if __name__ == '__main__': app.run(host="127.0.0.1", port=5000)