Created
September 27, 2021 12:33
-
-
Save shravankumar147/1cd2d62d74f7b433bac670e339a12c97 to your computer and use it in GitHub Desktop.
Execution Progress Animation using Threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "Execution Progress Animation using Threads", | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyPPlodMznmA9GaPhVetCmaU", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/shravankumar147/1cd2d62d74f7b433bac670e339a12c97/execution-progress-animation-using-threads.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "lM_DmgFLeCKj", | |
| "outputId": "fdd0f71c-27b5-43ad-aafc-3c7ef86b03bb" | |
| }, | |
| "source": [ | |
| "import itertools\n", | |
| "import threading\n", | |
| "import time\n", | |
| "import sys\n", | |
| "\n", | |
| "done = False\n", | |
| "#here is the animation\n", | |
| "def animate():\n", | |
| " for c in itertools.cycle(['|', '/', '-', '\\\\']):\n", | |
| " if done:\n", | |
| " break\n", | |
| " sys.stdout.write('\\rloading ' + c)\n", | |
| " sys.stdout.flush()\n", | |
| " time.sleep(0.1)\n", | |
| " sys.stdout.write('\\rDone! ')\n", | |
| "\n", | |
| "t = threading.Thread(target=animate)\n", | |
| "t.start()\n", | |
| "\n", | |
| "#long process here\n", | |
| "time.sleep(10)\n", | |
| "done = True" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Done! " | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "rmzBTCKZewY3", | |
| "outputId": "dba594c8-3178-46af-ad98-f4034bb9d93e" | |
| }, | |
| "source": [ | |
| "import sys, time, threading\n", | |
| "\n", | |
| "# Here is an example of the process function:\n", | |
| "\n", | |
| "def the_process_function():\n", | |
| " n = 20\n", | |
| " for i in range(n):\n", | |
| " time.sleep(1)\n", | |
| " # sys.stdout.write('\\r'+'loading... process '+str(i)+'/'+str(n)+' '+ '{:.2f}'.format(i/n*100)+'%')\n", | |
| " # sys.stdout.flush()\n", | |
| " # sys.stdout.write('\\r'+'loading... finished \\n')\n", | |
| "\n", | |
| "def animated_loading():\n", | |
| " chars = \"/—\\|\" \n", | |
| " for char in chars:\n", | |
| " sys.stdout.write('\\r'+'loading...'+char)\n", | |
| " time.sleep(.1)\n", | |
| " sys.stdout.flush()\n", | |
| "\n", | |
| "\n", | |
| "the_process = threading.Thread(name='process', target=the_process_function)\n", | |
| "the_process.start()\n", | |
| "\n", | |
| "while the_process.isAlive():\n", | |
| " animated_loading()\n" | |
| ], | |
| "execution_count": 2, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "loading...|" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "ctZe4eRnfa3j", | |
| "outputId": "04d95995-4806-4764-e0b4-803abf3c1776" | |
| }, | |
| "source": [ | |
| "from itertools import cycle\n", | |
| "from shutil import get_terminal_size\n", | |
| "from threading import Thread\n", | |
| "from time import sleep\n", | |
| "\n", | |
| "class Loader:\n", | |
| " def __init__(self, desc=\"Loading...\", end=\"Done!\", timeout=0.1):\n", | |
| " \"\"\"\n", | |
| " A loader-like context manager\n", | |
| "\n", | |
| " Args:\n", | |
| " desc (str, optional): The loader's description. Defaults to \"Loading...\".\n", | |
| " end (str, optional): Final print. Defaults to \"Done!\".\n", | |
| " timeout (float, optional): Sleep time between prints. Defaults to 0.1.\n", | |
| " \"\"\"\n", | |
| " self.desc = desc\n", | |
| " self.end = end\n", | |
| " self.timeout = timeout\n", | |
| "\n", | |
| " self._thread = Thread(target=self._animate, daemon=True)\n", | |
| " # self.steps = ['|', '/', '-', '\\\\', '|']\n", | |
| " #self.steps = [\"[■□□□□□□□□□]\",\"[■■□□□□□□□□]\", \"[■■■□□□□□□□]\", \"[■■■■□□□□□□]\", \"[■■■■■□□□□□]\", \"[■■■■■■□□□□]\", \"[■■■■■■■□□□]\", \"[■■■■■■■■□□]\", \"[■■■■■■■■■□]\", \"[■■■■■■■■■■]\"]\n", | |
| " self.steps = [\"⢿\", \"⣻\", \"⣽\", \"⣾\", \"⣷\", \"⣯\", \"⣟\", \"⡿\"]\n", | |
| " self.done = False\n", | |
| "\n", | |
| " def start(self):\n", | |
| " self._thread.start()\n", | |
| " return self\n", | |
| "\n", | |
| " def _animate(self):\n", | |
| " for c in cycle(self.steps):\n", | |
| " if self.done:\n", | |
| " break\n", | |
| " print(f\"\\r{self.desc} {c}\", flush=True, end=\"\")\n", | |
| " sleep(self.timeout)\n", | |
| "\n", | |
| " def __enter__(self):\n", | |
| " self.start()\n", | |
| "\n", | |
| " def stop(self):\n", | |
| " self.done = True\n", | |
| " cols = get_terminal_size((80, 20)).columns\n", | |
| " print(\"\\r\" + \" \" * cols, end=\"\", flush=True)\n", | |
| " print(f\"\\r{self.end}\", flush=True)\n", | |
| "\n", | |
| " def __exit__(self, exc_type, exc_value, tb):\n", | |
| " # handle exceptions with those variables ^\n", | |
| " self.stop()\n", | |
| "\n", | |
| "\n", | |
| "\n", | |
| "def my_function1():\n", | |
| " \"This can be any function execution\"\n", | |
| " n = 20\n", | |
| " for i in range(n):\n", | |
| " time.sleep(1)\n", | |
| " print(f\" I am first function :: I took {i+1} seconds to finish\")\n", | |
| "\n", | |
| "def my_function2():\n", | |
| " \"This can be any function execution\"\n", | |
| " n = 20\n", | |
| " for i in range(n):\n", | |
| " time.sleep(1)\n", | |
| " print(f\" I am second function :: I took {i+1} seconds to finish\") \n", | |
| "\n", | |
| "if __name__ == \"__main__\":\n", | |
| " with Loader(\"Execution in progress... :: \"):\n", | |
| " my_function1()\n", | |
| " sleep(1)\n", | |
| " my_function2()" | |
| ], | |
| "execution_count": 3, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Execution in progress... :: ⣯ I am first function :: I took 20 seconds to finish\n", | |
| "Execution in progress... :: ⣾ I am second function :: I took 20 seconds to finish\n", | |
| "Done!\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "h4RmlS5phleV", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "1753c67a-8787-425e-c029-f5f1e7a62119" | |
| }, | |
| "source": [ | |
| "with Loader(\"Execution in progress... :: \"):\n", | |
| " my_function1()\n", | |
| " sleep(1)\n", | |
| " my_function2()" | |
| ], | |
| "execution_count": 4, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Execution in progress... :: ⣯ I am first function :: I took 20 seconds to finish\n", | |
| "Execution in progress... :: ⣷ I am second function :: I took 20 seconds to finish\n", | |
| "Done!\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "dEaNRuleNUSb" | |
| }, | |
| "source": [ | |
| "" | |
| ], | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment