Plotly Dash:使用Docker Compose时回调中的图形未更新

时间:2019-11-21 11:02:00

标签: mongodb docker docker-compose plotly plotly-dash

我正在使用docker compose启动MongoDB,然后启动前端Dash。在docker-compose外本地启动时,图形将随着数据库中我的数据更新而更新,但是在使用docker compose时不会发生这种情况。

我发现我有日期滑动条和版本选择这两个功能,它们是从在其功能之外定义的全局变量中获取数据的,并且可以按预期运行,但是图形从其内部定义了数据。功能,并且不会像数据一样更新。

请参阅下面的docker文件和一些代码示例:

docker-compose:

version: '3.7'

services:
  db:
    image: "mongo:3.4.21-xenial"
    container_name: "jmongo_db"
    restart: always
    ports:
      - "27017:27017"
  frontend:
    image: frontend:version-1.0.0
    restart: always
    volumes:
      - "/data_ssd/r:/data_ssd/r"
    depends_on:
      - db
    ports:
      - "8050:8050"

Dockerfile:

FROM python:3.7

RUN useradd -ms /bin/bash product

ADD /src /src
ADD requirements.txt .
ADD docker-entrypoint.sh .
ADD scripts/execute-etl.sh .
RUN chmod +x /docker-entrypoint.sh
RUN chmod +x /execute-etl.sh
RUN touch /var/log/cron.log

RUN pip3 install -r requirements.txt

# Add AWS stuff inside docker
RUN curl -L https://packagecloud.io/segment/aws-okta/gpgkey | apt-key add -
ADD aws_config/segment_aws-okta.list /etc/apt/sources.list.d/segment_aws-okta.list
COPY system_requirements.txt .
RUN apt-get update \
    && cat system_requirements.txt | xargs apt-get install -y \
    && rm -rf /var/lib/apt/lists/*
ADD aws_config/config /home/product/.aws/config
ENV PYTHONPATH "${PYTHONPATH}:/src/:/product_metrics/"

EXPOSE 8050

ENTRYPOINT ["/docker-entrypoint.sh"]

docker_entrypoint.sh

#!/bin/bash
set -e

echo "Product Metrics Dashboard"

if [[ "$@" = "--shell" ]]
then
    echo "Going to run bash.";
    exec "/bin/bash"
else
    echo "Dashboard"
    pushd "/src/product_metrics/frontend/"
    exec stdbuf -o L -e L python3 /src/product_metrics/frontend/ldn_cartest_metrics.py
    popd
fi

可以工作的示例代码:

SW_VERSION_OPTIONS = [{'label': SW_VERSIONS[i], 'value': SW_VERSIONS[i]} for i in range(0, len(SW_VERSIONS))]
DATE_RANGE = conn.find_date_range()
...
...
...
                        dcc.RangeSlider(
                            id="date_slider",
                            min=DATE_RANGE[0],
                            max=DATE_RANGE[1],
                            value=[DATE_RANGE[0], DATE_RANGE[1]],
                            allowCross=False,
                            className="dcc_control",
                        ),
                        html.Div(id='output-container-range-slider'),
                        html.P("Filter by Release Version:", className="control_label"),
                        dcc.RadioItems(
                            id="release_selector",
                            options=[
                                {"label": "All ", "value": "all"},
                            ],
...
...
...
# Slider Display
@app.callback(
    dash.dependencies.Output("output-container-range-slider", 'children'),
    [dash.dependencies.Input('date_slider', 'value')])
def update_output(value):
    return f'You have selected date range: {[datetime.fromtimestamp(i).strftime("%d-%m-%Y") for i in value]}'

# Button on selection pannel
@app.callback(dash.dependencies.Output("release_version", "value"),
              [dash.dependencies.Input("release_selector", "value")])
def mass_release_selection(selector):
    if selector == "all":
        return SW_VERSIONS

无效的示例代码:

                        html.Div(
                            [dcc.Graph(id="planned_vs_unplanned_graph")],
                            id="count_graph",
                            className="pretty_container",
                        ),
...
...
...
@app.callback(
    dash.dependencies.Output('planned_vs_unplanned_graph', 'figure'),
    [dash.dependencies.Input('date_slider', 'value'),
     dash.dependencies.Input('release_version', 'value')])
def total_disengagements(date_range, releases):
    df = conn.planned_vs_unplanned()
    dff = filter_dataframe(df, releases, date_range)
    layout_aggregate = copy.deepcopy(layout)

    x_axis = dff['ticket']
    data = [
        dict(
            type="bar",
            name="Planned",
            x=x_axis,
            y=dff['planned_count'],
            line=dict(shape="spline", smoothing="2", color="#F9ADA0"),
        ),
        dict(
            type="bar",
            name="Unplanned ",
            x=x_axis,
            y=dff["unplanned_count"],
            line=dict(shape="spline", smoothing="2", color="#849E68"),
        ),
    ]
    layout_aggregate["title"] = "Planned vs Unplanned"

    figure = dict(data=data, layout=layout_aggregate)
    return figure

0 个答案:

没有答案