# Define app.callback in a separate file

**URL:** <https://community.plotly.com/t/define-app-callback-in-a-separate-file/67879>\
**Category:** Dash Python\
**Created:** [September 12, 2022, 5:35pm UTC](https://community.plotly.com/t/define-app-callback-in-a-separate-file/67879 "2022-09-12T17:35:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shwiftyRick](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@shwiftyRick](https://community.plotly.com/u/shwiftyRick)\
**Post date:** [September 12, 2022, 5:35pm UTC](https://community.plotly.com/t/define-app-callback-in-a-separate-file/67879/1 "2022-09-12T17:35:55Z")

</div>

My app is defined with a layout, followed by an @app.callback(), and then a function, as is standard practice. Is it possible, however, to define the callback statement and the function in a separate file?

I would like to be able to have my script read something like this:

my\_app.py

```auto
import external_file

app.layout = [LAYOUT HERE]
@app.callback(external_file().callback)
def do_stuff():
  ...

```

external\_file.py

```auto
class callback():
  def __init__ (self):
    callback = Output(), [Input()])

```

I didn’t show it above for brevity, but it would be ideal to have the function definition following the app.callback in the external file as well.

---

<div class="post-metadata">

**Author:** ![3d65](https://avatars.discourse-cdn.com/v4/letter/3/e99b99/32.png) [@3d65](https://community.plotly.com/u/3d65)\
**Post date:** [September 12, 2022, 6:34pm UTC](https://community.plotly.com/t/define-app-callback-in-a-separate-file/67879/2 "2022-09-12T18:34:18Z")

</div>

Sure, you can do that. Here’s an example:

main.py

```auto
from dash import Dash, html, dcc, callback
from external_file import PongCallback

app = Dash( __name__ )

app.layout = html.Div([
    dcc.Interval(id='interval', interval=1000),
    html.Div('PING', id='main-div')
])

# Pong callback
callback(*PongCallback.callback_params)(PongCallback())

if __name__ == " __main__":
    app.run(debug=True)

```

external\_file.py

```auto
from dash import Input, Output
from dash.exceptions import PreventUpdate

class PongCallback:
    callback_params = (
        [Output('main-div', 'children')],
        [Input('interval', 'n_intervals')]
    )

    @staticmethod
    def callback(n_intervals):
        if n_intervals is None:
            raise PreventUpdate

        if n_intervals % 2 == 0:
            return ['PONG']
        return ['PING']

    def __call__ (self, *args):
        return self.callback(*args)

```
