def run_query(df, query, context_vars=None, ignore_empty=False, pct=100, pct_type="random"): """ Utility function for running :func:`pandas:pandas.DataFrame.query` . This function contains extra logic to handle when column names contain special characters. Looks like pandas will be handling this in a future version: https://github.com/pandas-dev/pandas/issues/27017 The logic to handle these special characters in the meantime is only available in Python 3+ :param df: input dataframe :type df: :class:`pandas:pandas.DataFrame` :param query: query string :type query: str :param context_vars: dictionary of user-defined variables which can be referenced by name in query strings :type context_vars: dict, optional :param pct: random percentage of dataframe to load :type pct: int, optional :return: filtered dataframe """ def _load_pct(df): if pct is not None and pct < 100: if pct_type == "random": return df.sample(frac=pct / 100.0) records = int(len(df) * (pct / 100.0)) return df.head(records) if pct_type == "head" else df.tail(records) return df if (query or "") == "": return _load_pct(df) is_pandas25 = parse_version(pd.__version__) >= parse_version("0.25.0") curr_app_settings = global_state.get_app_settings() engine = curr_app_settings.get("query_engine", "python") df = df.query( query if is_pandas25 else query.replace("`", ""), local_dict=context_vars or {}, engine=engine, ) if not len(df) and not ignore_empty: raise Exception('query "{}" found no data, please alter'.format(query)) return _load_pct(df)
def load_app_settings(config): if config is None: return curr_app_settings = global_state.get_app_settings() theme = get_config_val(config, curr_app_settings, "theme", section="app") pin_menu = get_config_val( config, curr_app_settings, "pin_menu", section="app", getter="getboolean" ) language = get_config_val(config, curr_app_settings, "language", section="app") github_fork = get_config_val( config, curr_app_settings, "github_fork", section="app", getter="getboolean", ) hide_shutdown = get_config_val( config, curr_app_settings, "hide_shutdown", section="app", getter="getboolean", ) max_column_width = get_config_val( config, curr_app_settings, "max_column_width", section="app", getter="getint" ) max_row_height = get_config_val( config, curr_app_settings, "max_row_height", section="app", getter="getint" ) main_title = get_config_val(config, curr_app_settings, "main_title", section="app") main_title_font = get_config_val( config, curr_app_settings, "main_title_font", section="app" ) global_state.set_app_settings( dict( theme=theme, pin_menu=pin_menu, language=language, github_fork=github_fork, hide_shutdown=hide_shutdown, max_column_width=max_column_width, max_row_height=max_row_height, main_title=main_title, main_title_font=main_title_font, ) )
def load_app_settings(config): if config is None: return curr_app_settings = global_state.get_app_settings() theme = get_config_val(config, curr_app_settings, "theme", section="app") github_fork = get_config_val( config, curr_app_settings, "github_fork", section="app", getter="getboolean", ) hide_shutdown = get_config_val( config, curr_app_settings, "hide_shutdown", section="app", getter="getboolean", ) global_state.set_app_settings( dict(theme=theme, github_fork=github_fork, hide_shutdown=hide_shutdown) )
def load_app_settings(config): if config is None: return curr_app_settings = global_state.get_app_settings() theme = get_config_val(config, curr_app_settings, "theme", section="app") pin_menu = get_config_val(config, curr_app_settings, "pin_menu", section="app", getter="getboolean") language = get_config_val(config, curr_app_settings, "language", section="app") github_fork = get_config_val( config, curr_app_settings, "github_fork", section="app", getter="getboolean", ) hide_shutdown = get_config_val( config, curr_app_settings, "hide_shutdown", section="app", getter="getboolean", ) max_column_width = get_config_val(config, curr_app_settings, "max_column_width", section="app", getter="getint") max_row_height = get_config_val(config, curr_app_settings, "max_row_height", section="app", getter="getint") main_title = get_config_val(config, curr_app_settings, "main_title", section="app") main_title_font = get_config_val(config, curr_app_settings, "main_title_font", section="app") query_engine = get_config_val(config, curr_app_settings, "query_engine", section="app") open_custom_filter_on_startup = get_config_val( config, curr_app_settings, "open_custom_filter_on_startup", section="app", getter="getboolean", ) open_predefined_filters_on_startup = get_config_val( config, curr_app_settings, "open_predefined_filters_on_startup", section="app", getter="getboolean", ) hide_drop_rows = get_config_val( config, curr_app_settings, "hide_drop_rows", section="app", getter="getboolean", ) global_state.set_app_settings( dict( theme=theme, pin_menu=pin_menu, language=language, github_fork=github_fork, hide_shutdown=hide_shutdown, max_column_width=max_column_width, max_row_height=max_row_height, main_title=main_title, main_title_font=main_title_font, query_engine=query_engine, open_custom_filter_on_startup=open_custom_filter_on_startup, open_predefined_filters_on_startup= open_predefined_filters_on_startup, hide_drop_rows=hide_drop_rows, ))
def text(key): curr_lang = global_state.get_app_settings()["language"] return _languages.get(curr_lang, {}).get(key) or key