# Progress bar implementation or itterative text updates to an html.Div as the porcess executes

**URL:** <https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019>\
**Category:** Dash Python\
**Tags:** question\
**Created:** [April 25, 2024, 4:12am UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019 "2024-04-25T04:12:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vnavdulov](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vnavdulov](https://community.plotly.com/u/vnavdulov)\
**Post date:** [April 25, 2024, 4:12am UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019/1 "2024-04-25T04:12:42Z")

</div>

Has anyone had success with the implementation of depicting progress in layouts? I want the text from the function to display in the modal as the embedding process is happening. Since it takes a while with a large batch of documents, I don’t want the user to sit there and wonder if the process is still happening and not know when it’s going to end, however, I can’t return anything until the process ends. Ideally I would like to display the progress bar and the print statements below in a modal. I am not an advanced developer, so any help would be very useful. Thank you!

```auto
class CustomEmbeddings:
    def __init__ (self, model_id='e5-small-v2'):
        print("Initializing the embedding model...")
        self.model, self.tokenizer = self.load_model_and_tokenizer(model_id)
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        self.model.to(self.device)
        print(f"Model moved to device: {self.device}")

    def load_model_and_tokenizer(self, model_id):
        project_root = os.path.dirname(os.path.dirname( __file__ ))
        model_dir = os.path.join(project_root, 'embeddings', 'models')
        print(f"Loading model and tokenizer from {model_dir} for model ID: {model_id}...")
        model = AutoModel.from_pretrained(os.path.join(model_dir, model_id))
        tokenizer = AutoTokenizer.from_pretrained(os.path.join(model_dir, model_id))
        print("Model and tokenizer successfully loaded.")
        return model, tokenizer

    def embed_documents(self, texts):
        print("Starting the embedding process...")
        self.model.eval()
        embeddings = []
        with torch.no_grad():
            for i, text in enumerate(texts):
                tokens = self.tokenizer(text.page_content, return_tensors="pt", padding=True, truncation=True, max_length=512).to(
                    self.device)
                output = self.model(**tokens)
                sum_embeddings = (output.last_hidden_state * tokens['attention_mask'].unsqueeze(-1)).sum(1)
                normalized_embeddings = sum_embeddings / tokens['attention_mask'].sum(1, keepdim=True)
                embeddings.append(normalized_embeddings.cpu().numpy().tolist()[0])
                if (i + 1) % 10 == 0 or i == len(texts) - 1:
                    print(f"Processed {i + 1}/{len(texts)} texts")
        print("Embedding process completed.")
        return embeddings

    def embed_query(self, query):
        return self.embed_documents([query])[0]

```

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 25, 2024, 4:33am UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019/2 "2024-04-25T04:33:50Z")

</div>

Hey @vnavdulov, does this help?

> [@Show a progress bar in a modal](https://community.plotly.com/t/show-a-progress-bar-in-a-modal/78620):
>
> Hi there, I have a fairly large for- loop in one of the callbacks of my app and I was looking for a possibility to show a progress bar based on the progress in the for loop. I also wanted to be able to cancel the callback. I tweaked the [example](https://dash.plotly.com/background-callbacks#example-4:-progress-bar) of the background callbacks a bit, this is the result. Maybe it helps somebody looking for something similar. [gif]

---

<div class="post-metadata">

**Author:** ![vnavdulov](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vnavdulov](https://community.plotly.com/u/vnavdulov)\
**Post date:** [April 25, 2024, 3:26pm UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019/3 "2024-04-25T15:26:26Z")

</div>

Hello @AIMPED , thank you for pointing me in that direction. I was thinking of trying that out but I would like the messages generated in my function to appear in the modal that progress happens. Although even just having the progress bar would be very helpful as well. I’ll start there

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [April 25, 2024, 4:38pm UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019/4 "2024-04-25T16:38:14Z")

</div>

I think this might be even more interesting. I was blinded by the progress-bar and the modal…

> [@New websocket component: DashSocketIO](https://community.plotly.com/t/new-websocket-component-dashsocketio/82774):
>
> Hello everyone wave I have just created a new dash component [dash-socketio](https://github.com/renaudln/dash_socketio), to go hand-in-hand with flask-socketio. It allows to do some pretty cool things with Dash like notifying the user of progress while a callback is running, or streaming outputs while a callback is still running. See below a GIF of usage.py in the repo. This is a toy example with the above 2 usecases implemented. [dash-socketio-usage] Hopefully this can be useful to some of the community!

---

<div class="post-metadata">

**Author:** ![vnavdulov](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vnavdulov](https://community.plotly.com/u/vnavdulov)\
**Post date:** [April 25, 2024, 9:31pm UTC](https://community.plotly.com/t/progress-bar-implementation-or-itterative-text-updates-to-an-html-div-as-the-porcess-executes/84019/5 "2024-04-25T21:31:50Z")

</div>

@AIMPED That’s perfect! Just what I was looking for. Thank you!
