Skip to main content

Integrates ChartFactor with Jupyter Notebooks

Project description

ChartFactor for Python

How to install and test

After download the project go to the root directory

Uninstall any previous installation

python -m pip uninstall chartfactor --yes

Build the chartfactor package like this

python setup.py sdist

The previous command will create a dist folder with tha package in the chartfactor-1.0.tar.gz file.

Install the package locally

python -m pip install ./dist/chartfactor-1.0.tar.gz

Usage

Go to the Jupyter Notebook and in a new python notebook do the following

Cell 1

from chartfactor import cf
import pandas as pd

Cell 2

ts = pd.read_csv('./csvs/ticket_sales.csv')

Cell 3

jsonConfig = {
    "showRowNumber": False,
    "autoSizeColumns": False,
    "config": {
        "filters": [
            # {
            #     "path": "company",
            #     "operation": "TS",
            #     "value": [
            #         "pedia"
            #     ]
            # }
        ],
        "groups": [
            {
                "name": "catname",
                "label": "catname",
                "sort": {
                    "name": "commission",
                    "func": "sum",
                    "dir": "desc"
                },
                "limit": 10
            },
            {
                "name": "eventname",
                "label": "eventname",
                "sort": {
                    "name": "eventname",
                    "func": "unique",
                    "dir": "asc"
                },
                "limit": 5
            }
        ],
        "metrics": [
            {
                "name": "pricepaid",
                "func": "sum"
            }
        ],
        "fields": [

        ],
        "exclude": []
    },
    "rows": [],
    "limit": 8,
    "offset": 0,
    "visualization" : "Bars"
}

Cell 4

prov = cf.provider(ts, jsonConfig)
# prov.get_config()
# prov.run_count_query()
# prov.run_raw_query()
prov.visualize()

How to run test

Inside the test folder execute the following command:

python -m unittest

Increase jupyter notebook data rate limit

This may only be required on Windows environments. Run the following command in a terminal to generate the jupyter_notebook_config.py file if not exists:

jupyter notebook --generate-config

After that, locate the file in the .jupyter folder and add the following line at the end:

c.NotebookApp.iopub_data_rate_limit = 1.0e10

Configuring deployment credentials

Create and configure ~/.pypirc which is the pypi.org configuration file, in order to set the user and the token to upload the package.

  1. Create the file:
touch ~/.pypirc
  1. Assign permissions:
chmod 600 ~/.pypirc
  1. Open the file:
open ~/.pypirc
  1. Add this configuration:
[distutils]
index-servers =
    pypi
    testpypi

[pypi]
username = __token__
repository = https://upload.pypi.org/legacy/

[testpypi]
username = __token__
repository = https://test.pypi.org/legacy/

Notice that instead of using the password field, we are going to save the API tokens and passwords securely using keyring (which is installed by Twine):

  1. Configuring the test server:
keyring set https://test.pypi.org/legacy/ __token__
  1. Enter the generated token for the test environment:
Password for '__token__' in 'https://test.pypi.org/legacy/': TOKEN
  1. Configuring the production server:
keyring set https://upload.pypi.org/legacy/ __token__
  1. Enter the generated token for the production environment:
Password for '__token__' in 'https://upload.pypi.org/legacy/': TOKEN

Install Python Package Dependencies

To build the project successfully

pip install -r requirements.txt

To load packages for developers

pip install -r requirements-dev.txt

The execution of the command above will install if not exists the following packages:

  • twine>=3.7.1
  • build>=0.7.0
  • wheel>=0.37.1

If is necessary to upgrade the packages to a newer version just run the following command:

python3 -m pip install --upgrade {twine|build|wheel}

Code Obfuscation

The code obfuscation allows to modify a program code so that it is no longer useful to a hacker but remains fully functional.

Two processes to achieve code obfuscation and its corresponding deployment are detailed below.

Using pyarmor package to build a pyarmored wheel (Recommended)

Installing Anaconda

In order to create easily the virtual environments with the different python versions, and also obfuscate the code and be able to run the chartfactor-py package in every python version, it's necessary to install the Anaconda software.

First download it from the official website.

Next, open the software, go to the Environments tab, and create two virtual environment for python 3.6 and 3.7 with the following names:

  • env3.6
  • env3.7

Executing the build-pyarmored-wheel.sh file

The build-pyarmored-wheel.sh file was created to automatically build, obfuscate and deploy the package to the pypi.org server. After installing Anaconda successfully and created the virtual environments, execute the following command inside the chartfactor-py folder:

sh build-pyarmored-wheel.sh -v 1.x <-t|-p>

Flags:

  • -v package version
  • -t upload package to the test environment
  • -p upload package to the production environment

Using pyc-wheel package

This package allows you to compile all py files in a wheel to pyc files. For more information go to the pyc-wheel page

In the chartfactor-py folder execute the following build command:

python3 -m build

After execution, 2 files should have been generated in the dist folder:

  • chartfactor-1.x.tar.gz
  • chartfactor-1.x-py3-none-any.whl

Next, the following command must be executed to convert the .py files to .pyc (compiled and with the code obfuscated) inside the .whl file.

python -m pyc_wheel dist/chartfactor-1.x-py3-none-any.whl

You can add this option --with_backup to create a backup during the process.

Once the .whl file has been processed, it must be uploaded to the pypi.org or test.pypi.org repository, whichever the case may be.

To achieve that do the following:

Test server

python3 -m twine upload --repository testpypi ./*

Production server

python3 -m twine upload dist/*

For each case you must enter the user name __token__ and the provided token after.

To install the package run the following command:

pip uninstall chartfactor

Installing the Chartfactor.py package as an end-user

From test server

pip install -i https://test.pypi.org/simple/ chartfactor==1.x

From production server

python3 -m pip install chartfactor

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chartfactor-3.0.12.tar.gz (190.0 kB view hashes)

Uploaded Source

Built Distribution

chartfactor-3.0.12-py3-none-any.whl (5.8 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page