🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Dash crashes by plotting a simple Matlplotlib

I am trying to plot a figure however dash crashes with the following message

Running on http://127.0.0.1:8050/
Debugger PIN: 887-310-245
2019-01-23 23:31:39.075 Python[97729:22380712] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /BuildRoot/Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1454.90/Foundation/Misc.subproj/NSUndoManager.m:361
2019-01-23 23:31:39.078 Python[97729:22380712] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff475df2db __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fff6e782c76 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff475e5072 +[NSException raise:format:arguments:] + 98
	3   Foundation                          0x00007fff4970e340 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
	4   Foundation                          0x00007fff4969cf44 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 469
	5   AppKit                              0x00007fff44ae996e -[NSApplication run] + 997
	6   _macosx.cpython-37m-darwin.so       0x000000011ac5cd72 show + 210
	7   Python                              0x000000010a6e7372 _PyMethodDef_RawFastCallKeywords + 591
	8   Python                              0x000000010a6e68af _PyCFunction_FastCallKeywords + 44
	9   Python                              0x000000010a77cb2b call_function + 636
	10  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	11  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	12  Python                              0x000000010a77cba0 call_function + 753
	13  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	14  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	15  Python                              0x000000010a6e64dd _PyFunction_FastCallDict + 441
	16  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	17  Python                              0x000000010a6e69bd PyObject_Call + 136
	18  Python                              0x000000010a775a88 _PyEval_EvalFrameDefault + 7807
	19  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	20  Python                              0x000000010a6e6874 _PyFunction_FastCallKeywords + 225
	21  Python                              0x000000010a77cba0 call_function + 753
	22  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	23  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	24  Python                              0x000000010a6e6874 _PyFunction_FastCallKeywords + 225
	25  Python                              0x000000010a77cba0 call_function + 753
	26  Python                              0x000000010a7758b5 _PyEval_EvalFrameDefault + 7340
	27  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	28  Python                              0x000000010a775a88 _PyEval_EvalFrameDefault + 7807
	29  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	30  Python                              0x000000010a6e64dd _PyFunction_FastCallDict + 441
	31  Python                              0x000000010a775a88 _PyEval_EvalFrameDefault + 7807
	32  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	33  Python                              0x000000010a6e64dd _PyFunction_FastCallDict + 441
	34  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	35  Python                              0x000000010a6e69bd PyObject_Call + 136
	36  Python                              0x000000010a775a88 _PyEval_EvalFrameDefault + 7807
	37  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	38  Python                              0x000000010a77cba0 call_function + 753
	39  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	40  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	41  Python                              0x000000010a77cba0 call_function + 753
	42  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	43  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	44  Python                              0x000000010a77cba0 call_function + 753
	45  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	46  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	47  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	48  Python                              0x000000010a72455c slot_tp_call + 71
	49  Python                              0x000000010a6e6715 _PyObject_FastCallKeywords + 359
	50  Python                              0x000000010a77cb99 call_function + 746
	51  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	52  Python                              0x000000010a6f24bf gen_send_ex + 242
	53  Python                              0x000000010a774f38 _PyEval_EvalFrameDefault + 4911
	54  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	55  Python                              0x000000010a6e6874 _PyFunction_FastCallKeywords + 225
	56  Python                              0x000000010a77cba0 call_function + 753
	57  Python                              0x000000010a77580f _PyEval_EvalFrameDefault + 7174
	58  Python                              0x000000010a77d432 _PyEval_EvalCodeWithName + 1835
	59  Python                              0x000000010a6e6874 _PyFunction_FastCallKeywords + 225
	60  Python                              0x000000010a77cba0 call_function + 753
	61  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	62  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	63  Python                              0x000000010a77cba0 call_function + 753
	64  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	65  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	66  Python                              0x000000010a77cba0 call_function + 753
	67  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	68  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	69  Python                              0x000000010a77cba0 call_function + 753
	70  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	71  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	72  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	73  Python                              0x000000010a724f57 slot_tp_init + 80
	74  Python                              0x000000010a721c23 type_call + 178
	75  Python                              0x000000010a6e6715 _PyObject_FastCallKeywords + 359
	76  Python                              0x000000010a77cb99 call_function + 746
	77  Python                              0x000000010a775771 _PyEval_EvalFrameDefault + 7016
	78  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	79  Python                              0x000000010a77cba0 call_function + 753
	80  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	81  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	82  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	83  Python                              0x000000010a6e69bd PyObject_Call + 136
	84  Python                              0x000000010a775a88 _PyEval_EvalFrameDefault + 7807
	85  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	86  Python                              0x000000010a77cba0 call_function + 753
	87  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	88  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	89  Python                              0x000000010a77cba0 call_function + 753
	90  Python                              0x000000010a775758 _PyEval_EvalFrameDefault + 6991
	91  Python                              0x000000010a6e6c8a function_code_fastcall + 112
	92  Python                              0x000000010a6e760d _PyObject_Call_Prepend + 150
	93  Python                              0x000000010a6e69bd PyObject_Call + 136
	94  Python                              0x000000010a7e32ca t_bootstrap + 71
	95  libsystem_pthread.dylib             0x00007fff6f6b4661 _pthread_body + 340
	96  libsystem_pthread.dylib             0x00007fff6f6b450d _pthread_body + 0
	97  libsystem_pthread.dylib             0x00007fff6f6b3bf9 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Process finished with exit code 250

here is the code

# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from io import BytesIO
import matplotlib.pyplot as plt
import base64
import numpy as np
import pickle
import shap


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

def fig_to_uri(in_fig, close_all=True, **save_args):
    # type: (plt.Figure) -> str
    """
    Save a figure as a URI
    :param in_fig:
    :return:
    """
    out_img = BytesIO()
    in_fig.savefig(out_img, format='png', **save_args)
    if close_all:
        in_fig.clf()
        plt.close('all')
    out_img.seek(0)  # rewind file
    encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
    return "data:image/png;base64,{}".format(encoded)



app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    dcc.Input(id='input-1-state', type='text', value='Montréal'),
    html.Button(id='submit-button', n_clicks=0, children='Submit'),
    html.Div(id='output-state'),
    html.Div([html.Img(id = 'cur_plot', src = '')],
             id='plot_div')
    ])


@app.callback(Output('output-state', 'children'),
              [Input('submit-button', 'n_clicks')],
              [State('input-1-state', 'value')])
def update_output(n_clicks, input1):
    return u'''
        The Button has been pressed {} times,
        Input 1 is "{}"
    '''.format(n_clicks, input1)


@app.callback(
    Output(component_id='cur_plot', component_property='src'),
    [Input(component_id='submit-button', component_property='n_clicks')]
)
def update_graph(input_value):

    #shap.force_plot(explainer.expected_value, shap_values[0,:], one_sample, matplotlib=True)

     t = np.arange(0.0, 2.0, 0.01)
     s = 1 + np.sin(2*np.pi*t)
     plt.plot(t, s)

     plt.xlabel('time (s)')
     plt.ylabel('voltage (mV)')
     plt.title('About as simple as it gets, folks')
     plt.grid(True)
    out_url = fig_to_uri(plt.show())
    return out_url

if __name__ == '__main__':
    app.run_server(debug=True)

## add the figure

the simple plot comes from here https://matplotlib.org/examples/pylab_examples/simple_plot.html

the problem was passing plot.show() instead of plt - one should mage sure to pass matlibplot figure type instead of a list or other object types.