dkutils.reporting.dataframe_wrapper module

class dkutils.reporting.dataframe_wrapper.DataFrameWrapper(connection_string: str)[source]

Bases: object

Class that facilitates running a query in a database via a Pandas Dataframe and then writing the data to files.

A number of formats are supported including png, html and text files.

create_report(query: str, query_type: str, filename=None, additional_parms=None)[source]

This method will execute the given query and then write out the results as an html, png or txt file depending on the given query_type. The file will named with the given filename. If the the filename parameter then the file will be named file_ddd.ext. Where ddd will be a three digit number with two leading zeros that is incremented for each time this method is called with a particular file type. The extension will be determined by the query_type as follows:

html = html plot = png text = txt

Parameters
  • query (str) – A string containing the query to be executed

  • query_type (str) – A string containing the type of file to be created. Must be either html, ping or text

  • filename (str, optional) – The filename to be assigned when saving the query results

  • additional_parms (dict, optional) – A dictionary containing additional the will be passed to the either to to_html, plot or to_string methods of the dataframe

Returns

  • int – A integer containing the number of rows returned by the query

  • str – A string containing the name of the file created

classmethod mssql(username: str, password: str, hostname: str, database: str, port: int = 1433)[source]

Create a DataFrame with a connection to a mssql database :param username: mssql username :type username: str :param password: mssql password :type password: str :param hostname: mssql host name :type hostname: str :param database: mssql database name :type database: str :param port: mssql port :type port: int, optional

Returns

a DataFramewrapper that can be used to execute queries against a PostgreSQL database

Return type

DataFrameWrapper

classmethod postgresql(username: str, password: str, hostname: str, database: str, port: int = 5432)[source]

Create a DataFrame with a connection to a postgreSQL database :param username: PostgreSQL username :type username: str :param password: PostgreSQL password :type password: str :param hostname: PostgreSQL host name :type hostname: str :param database: PostgreSQL database name :type database: str :param port: PostgreSQL port :type port: int, optional

Returns

a DataFramewrapper that can be used to execute queries against a PostgreSQL database

Return type

DataFrameWrapper

classmethod snowflake(username: str, password: str, snowflake_account: str, database: str, warehouse: str)[source]

Create a DataFrame with a connection to a Snowflake database :param username: Snowflake username :type username: str :param password: Snowflake password :type password: str :param snowflake_account: Snowflake account name :type snowflake_account: str :param database: Snowflake database name :type database: str :param warehouse: Snowflake warehouse name

Returns

a DataFramewrapper that can be used to execute queries against a snowflake database

Return type

DataFrameWrapper