# How to use --setup to schedule tasks

**URL:** <https://discourse.holoviz.org/t/how-to-use-setup-to-schedule-tasks/8977>\
**Category:** Showcase\
**Tags:** panel\
**Created:** [October 21, 2025, 7:40pm UTC](https://discourse.holoviz.org/t/how-to-use-setup-to-schedule-tasks/8977 "2025-10-21T19:40:57Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![ahuang11](https://discourse.holoviz.org/user_avatar/discourse.holoviz.org/ahuang11/32/4947_2.png) [@ahuang11](https://discourse.holoviz.org/u/ahuang11)\
**Post date:** [October 21, 2025, 7:40pm UTC](https://discourse.holoviz.org/t/how-to-use-setup-to-schedule-tasks/8977/1 "2025-10-21T19:40:57Z")

</div>

`app.py` creates and reuses example files when users click “Download Example File,” while `setup.py` runs alongside it, **clearing out cached files every 5 seconds** to keep the directory clean.

app.py

```python
# download button that writes a file to disk and serves it to the user using panel FileDownload callback

import random
import panel as pn
from pathlib import Path

pn.extension()

BASE_FILE_DIR = Path( __file__ ).parent / "examples"
BASE_FILE_DIR.mkdir(exist_ok=True)

def create_file():
    file_path = BASE_FILE_DIR / f"example_{random.randint(1, 3)}.txt"
    if file_path.exists():
        print("Reusing existing file:", file_path)
        return file_path

    with open(file_path, "w") as f:
        print("Creating new file:", file_path)
        f.write(
            "This is an example file. Random number: " + str(random.randint(1, 100))
        )
    return file_path

file_download = pn.widgets.FileDownload(
    filename="example.txt",
    button_type="primary",
    callback=create_file,
    label="Download Example File",
)
print("File download widget created:", file_download, flush=True)
pn.Column("# File Download Example", file_download).servable()

```

setup.py

```python
# Every 5 seconds, clears out the examples directory
import panel as pn
from pathlib import Path

BASE_FILE_DIR = Path( __file__ ).parent / "examples"
BASE_FILE_DIR.mkdir(exist_ok=True)

def setup():
    print("Starting periodic cleanup of example files...")
    for file in BASE_FILE_DIR.iterdir():
        if file.is_file():
            print(file)
            file.unlink()

pn.state.schedule_task('task', setup, period='5s')

```

Run:  
`panel serve app.py --setup setup.py`

---

_[View the full topic](https://discourse.holoviz.org/t/how-to-use-setup-to-schedule-tasks/8977)._
