Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #32: Available Halo in jupyter notebook #40

Merged
merged 7 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 228 additions & 0 deletions examples/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"\n",
"os.sys.path.append(os.path.dirname(os.path.abspath('./')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HaloNotebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from halo import HaloNotebook as Halo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test example codes\n",
"\n",
"This frontend (for example, a static rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>) doesn't currently support widgets. If you wonder results, run notebook manually.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `persist_spin.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"success_message = 'Loading success'\n",
"failed_message = 'Loading failed'\n",
"unicorn_message = 'Loading unicorn'\n",
"\n",
"spinner = Halo(text=success_message, spinner='dots')\n",
"\n",
"try:\n",
" spinner.start()\n",
" time.sleep(1)\n",
" spinner.succeed()\n",
" spinner.start(failed_message)\n",
" time.sleep(1)\n",
" spinner.fail()\n",
" spinner.start(unicorn_message)\n",
" time.sleep(1)\n",
" spinner.stop_and_persist({'symbol': '🦄'.encode('utf-8'), 'text': unicorn_message})\n",
"except (KeyboardInterrupt, SystemExit):\n",
" spinner.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `long_text.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spinner = Halo(text='This is a text that it is too long. In fact, it exceeds the eighty column standard '\n",
" 'terminal width, which forces the text frame renderer to add an ellipse at the end of the '\n",
" 'text. This should definitely make it more than 180!', spinner='dots', animation='marquee')\n",
"\n",
"try:\n",
" spinner.start()\n",
" time.sleep(5)\n",
" spinner.succeed('End!')\n",
"except (KeyboardInterrupt, SystemExit):\n",
" spinner.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `loader_spin.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"spinner = Halo(text='Downloading dataset.zip', spinner='dots')\n",
"\n",
"try:\n",
" spinner.start()\n",
" for i in range(100):\n",
" spinner.text = '{0}% Downloaded dataset.zip'.format(i)\n",
" time.sleep(0.05)\n",
" spinner.succeed('Downloaded dataset.zip')\n",
"except (KeyboardInterrupt, SystemExit):\n",
" spinner.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `doge_spin.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spinner = Halo(text='Such Spins', spinner='dots')\n",
"\n",
"try:\n",
" spinner.start()\n",
" time.sleep(1)\n",
" spinner.text = 'Much Colors'\n",
" spinner.color = 'magenta'\n",
" time.sleep(1)\n",
" spinner.text = 'Very emojis'\n",
" spinner.spinner = 'hearts'\n",
" time.sleep(1)\n",
" spinner.stop_and_persist({'symbol': '🦄 '.encode('utf-8'), 'text': 'Wow!'})\n",
"except (KeyboardInterrupt, SystemExit):\n",
" spinner.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `custom_spins.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spinner = Halo(\n",
" text='Custom Spins',\n",
" spinner={\n",
" 'interval': 100,\n",
" 'frames': ['-', '+', '*', '+', '-']\n",
" }\n",
")\n",
"\n",
"try:\n",
" spinner.start()\n",
" time.sleep(2)\n",
" spinner.succeed('It works!')\n",
"except (KeyboardInterrupt, SystemExit):\n",
" spinner.stop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `context_manager.py`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with Halo(text='Loading', spinner='dots'):\n",
" # Run time consuming work here\n",
" time.sleep(2)\n",
"\n",
"with Halo(text='Loading 2', spinner='dots') as spinner:\n",
" # Run time consuming work here\n",
" time.sleep(2)\n",
" spinner.succeed('Done!')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions halo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import logging

from .halo import Halo
from .halo_notebook import HaloNotebook

logging.getLogger(__name__).addHandler(logging.NullHandler())
85 changes: 85 additions & 0 deletions halo/halo_notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from __future__ import unicode_literals, absolute_import, print_function

import threading
import cursor
from halo import Halo
from halo._utils import decode_utf_8_text
from ipywidgets.widgets import Output
from IPython.display import display


class HaloNotebook(Halo):
CLEAR_LINE = '\033[K'

def __init__(self, text='', color='cyan', spinner=None, animation=None, interval=-1, enabled=True, stream=None):

super(HaloNotebook, self).__init__(text, color, spinner, animation, interval, enabled, stream)
self.output = self._make_output_widget()

def _make_output_widget(self):
return Output()

# TODO: using property and setter
def _output(self, text=''):
return ({'name': 'stdout', 'output_type': 'stream', 'text': text},)

def clear(self):
if not self._enabled:
return self

with self.output:
self.output.outputs += self._output('\r')
self.output.outputs += self._output(self.CLEAR_LINE)

self.output.outputs = self._output()
return self

def _render_frame(self):
frame = self.frame()
output = '\r{0}'.format(frame)
with self.output:
self.output.outputs += self._output(output)

def start(self, text=None):
if text is not None:
self._text = self._get_text(text, animation=None)

if not self._enabled or self._spinner_id is not None:
return self

if self._stream.isatty():
cursor.hide()

self.output = self._make_output_widget()
display(self.output)
self._stop_spinner = threading.Event()
self._spinner_thread = threading.Thread(target=self.render)
self._spinner_thread.setDaemon(True)
self._render_frame()
self._spinner_id = self._spinner_thread.name
self._spinner_thread.start()

return self

def stop_and_persist(self, options={}):
if type(options) is not dict:
raise TypeError('Options passed must be a dictionary')

if 'symbol' in options and options['symbol'] is not None:
symbol = decode_utf_8_text(options['symbol'])
else:
symbol = ' '

if 'text' in options and options['text'] is not None:
text = decode_utf_8_text(options['text'])
else:
text = self._text['original']

text = text.strip()

self.stop()

output = '\r{0} {1}\n'.format(symbol, text)

with self.output:
self.output.outputs = self._output(output)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ cursor==1.2.0
termcolor==1.1.0
colorama==0.3.9
six==1.11.0
ipywidgets==7.1.0
Loading