예제 #1
0
def get_column_visualisations(column,
                              col_data,
                              vis_scripts,
                              id='',
                              single_val=None,
                              context=None):
    """
    Given a column object and a dataframe, create the visualisations for
    this column. The list vis_scripts is modified to include the scripts to
    include in the HTML page. If single_val is not None, its position in
    the visualisation is marked (place individual value in population
    measure.
    :param column: Column element to visualize
    :param col_data: Data in the column (extracted from the data frame)
    :param id: String to use to label the visualization
    :param vis_scripts: Collection of visualisation scripts needed in HTML
    :param single_val: Mark a specific value (or None)
    :param context: Dictionary to pass to the rendering
    :return:
    """

    # Result to return
    visualizations = []

    # Initialize the context properly
    if context is None:
        context = {}

    # Create V1 if data type is integer or real
    if column.data_type == 'integer' or column.data_type == 'double':

        # Propagate the id if given
        if id:
            context['id'] = id + '_boxplot'

        if single_val is not None:
            context['individual_value'] = single_val
        v1 = PlotlyBoxPlot(data=col_data, context=context)
        v1.get_engine_scripts(vis_scripts)
        visualizations.append(v1)

    # Create V2
    # Propagate the id if given
    if id:
        context['id'] = id + '_histogram'

    if single_val is not None:
        context['individual_value'] = single_val
    v2 = PlotlyColumnHistogram(data=col_data, context=context)
    v2.get_engine_scripts(vis_scripts)
    visualizations.append(v2)

    return visualizations
예제 #2
0
def vis_html_content(context, column_name):
    # Get the action
    action = context.get(action_context_var, None)
    if not action:
        raise Exception(_('Action object not found when processing tag'))
    workflow = action.workflow

    # Check if the column is correct
    if not Column.objects.filter(workflow=workflow, name=column_name).exists():
        raise Exception(_('Column {0} does not exist').format(column_name))

    # Get the visualization number to generate unique IDs
    viz_number = context[viz_number_context_var]

    # Create the context for the visualization
    viz_ctx = {
        'style': 'width:400px; height:225px;',
        'id': 'viz_tag_{0}'.format(viz_number)
    }

    # If there is a column name in the context, insert it as individual value
    # If the template is simply being saved and rendered to detect syntax
    # errors, we may not have the data of an individual, so we have to relax
    # this restriction.
    ivalue = context.get(tr_item(column_name), None)
    if ivalue is not None:
        viz_ctx['individual_value'] = ivalue

    # Get the condition filter
    try:
        cond_filter = Condition.objects.get(action__id=action.id,
                                            is_filter=True)
    except ObjectDoesNotExist:
        cond_filter = None

    # Get the data from the data frame
    df = pandas_db.get_subframe(workflow.id, cond_filter, [column_name])
    # Get the visualisation
    viz = PlotlyColumnHistogram(data=df, context=viz_ctx)

    prefix = ''
    if viz_number == 0:
        prefix = ''.join([
            '<script src="{0}"></script>'.format(x)
            for x in PlotlyColumnHistogram.get_engine_scripts()
        ])

    # Update viz number
    context[viz_number_context_var] = viz_number + 1

    # Return the rendering of the viz marked as safe
    return mark_safe(prefix + viz.render())