flask.helpers get_root_path Example Code

get_root_path is a function within the flask.helpers module of the Flask framework. get_root_path returns the filesystem path to a package or the current working directly if the path cannot be found.

flash, make_response, safe_join, send_file, and url_for are several other callables with code examples from the same flask.helpers package.

Example 1 from indico

indico (project website, documentation and sandbox demo) is a Flask-based web app for event management. The code is open sourced under the MIT license.

indico / indico / cli / setup.py

# setup.py

import os
import re
import shutil
import socket
import sys
from operator import attrgetter
from pathlib import Path
from smtplib import SMTP

import click
from click import wrap_text
from flask.helpers import get_root_path
from pkg_resources import iter_entry_points
from prompt_toolkit import prompt
from prompt_toolkit.completion import PathCompleter, WordCompleter
from prompt_toolkit.styles import Style
from pytz import all_timezones, common_timezones
from redis import RedisError, StrictRedis
from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.pool import NullPool
from terminaltables import AsciiTable
from werkzeug.urls import url_parse

from indico.core.db.sqlalchemy.util.models import import_all_models
from indico.util.console import cformat
from indico.util.string import validate_email

def _echo(msg=''):
    click.echo(msg, err=True)

def _warn(msg):
    msg = wrap_text(msg)
    click.echo(click.style(msg, fg='yellow'), err=True)

## ... source file abbreviated to get to get_root_path examples ...

def _prompt_abort():
    _confirm('Continue anyway?', abort=True)

def _copy(src, dst, force=False):
    if not force and os.path.exists(dst):
        _echo(cformat('%{yellow!}{}%{reset}%{yellow} already exists; not copying %{yellow!}{}')
              .format(dst, src))
    _echo(cformat('%{green}Copying %{green!}{}%{reset}%{green} -> %{green!}{}').format(src, dst))
    shutil.copy(src, dst)

def _link(src, dst):
    _echo(cformat('%{cyan}Linking %{cyan!}{}%{reset}%{cyan} -> %{cyan!}{}').format(dst, src))
    if os.path.exists(dst) or os.path.islink(dst):
    os.symlink(src, dst)

def _get_dirs(target_dir):
    if not os.path.isdir(target_dir):
        _echo(cformat('%{red}Directory not found:%{red!} {}').format(target_dir))
    return get_root_path('indico'), os.path.abspath(target_dir)

PROMPT_TOOLKIT_STYLE = Style.from_dict({
    'help': '#aaaaaa',
    'prompt': '#5f87ff',
    'default': '#dfafff',
    'bracket': '#ffffff',
    'colon': '#ffffff',
    '': '#aaffaa',  # user input

def _prompt(message, default='', path=False, list_=None, required=True, validate=None, allow_invalid=False,
            password=False, help=None):
    def _get_prompt_tokens():
        rv = [
            ('class:prompt', message),
            ('class:colon', ': '),
        if first and help:
            rv.insert(0, ('class:help', wrap_text(help) + '\n'))
        return rv

    completer = None

## ... source file abbreviated to get to get_root_path examples ...

        if dev:
            config_data += [
                '# Development options',
                'DB_LOG = True',
                'DEBUG = True',
                'SMTP_USE_CELERY = False'

        config = '\n'.join(x for x in config_data if x is not None)

        if dev:
            if not os.path.exists(self.data_root_path):

        for path in self._missing_dirs:
            _echo(cformat('%{magenta}Creating %{magenta!}{}%{reset}%{magenta}').format(path))

        _echo(cformat('%{magenta}Creating %{magenta!}{}%{reset}%{magenta}').format(self.config_path))
        with open(self.config_path, 'w') as f:
            f.write(config + '\n')

        package_root = get_root_path('indico')
        _copy(os.path.normpath(os.path.join(package_root, 'logging.yaml.sample')),
              os.path.join(self.config_dir_path, 'logging.yaml'))

        if not dev:
            _link(os.path.join(package_root, 'web', 'static'), os.path.join(self.data_root_path, 'web', 'static'))
            _copy(os.path.join(package_root, 'web', 'indico.wsgi'),
                  os.path.join(self.data_root_path, 'web', 'indico.wsgi'),

        if create_config_link:
            _link(self.config_path, config_link_path)

        _echo(cformat('%{green}Indico has been configured successfully!'))
        if not dev and not create_config_link:
            _echo(cformat('Run %{green!}export INDICO_CONFIG={}%{reset} to use your config file')

        if self.old_archive_dir:
            _echo(cformat('Check %{green!}https://git.io/vHP6o%{reset} for a guide on how to '
                          'import data from Indico v1.2'))
            _echo(cformat('You can now run %{green!}indico db prepare%{reset} to initialize your Indico database'))

## ... source file continues with no further get_root_path examples...

Sponsored By

Sentry logo

Software errors are inevitable. Chaos is not. Try Sentry for free.

AssemblyAI logo

The most accurate speech-to-text API. Built for Python developers.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2021