analitics

Pages

Friday, February 15, 2019

Install , test and fix error of the jupyter-book into python 3.

Jupyter Books lets you build an online book using a collection of Jupyter Notebooks and Markdown files. Its output is similar to the excellent Bookdown tool, and adds extra functionality for people running a Jupyter stack.
Read more about this on the official webpage.
Today I start to test this python module named jupyter-book.
I find some errors and I fixed to running well a demo jupyter-book instance.

C:\Python364\Scripts>pip install jupyter-book
Collecting jupyter-book
...
Installing collected packages: ruamel.yaml, jupyter-book
Successfully installed jupyter-book-0.4.1 ruamel.yaml-0.15.88
First I try to create a jupyter-book named catafest but I got this error:
C:\Python364>jupyter-book create catafest --demo
Traceback (most recent call last):
...
from nbclean import NotebookCleaner
ModuleNotFoundError: No module named 'nbclean'
The next step was to fix the error by install the nbclean python module and see all dependencies of python modules:
C:\Python364\Scripts>pip3.6.exe install nbclean
Collecting nbclean
...
Collecting nbgrader (from nbclean)
...
Collecting sqlalchemy (from nbgrader->nbclean)
...
Collecting alembic (from nbgrader->nbclean)
...
Collecting ipython<=6.2.1 (from nbgrader->nbclean)
...
Collecting jupyter-console<=5.2.0 (from nbgrader->nbclean)
...
Collecting Mako (from alembic->nbgrader->nbclean)
...
Collecting prompt-toolkit<2 .0.0="">=1.0.4 (from ipython<=6.2.1->nbgrader->nbclean)
...
Collecting prompt-toolkit<2 .0.0="">=1.0.4 (from ipython<=6.2.1->nbgrader->nbclean)
...
Building wheels for collected packages: nbgrader, sqlalchemy, alembic, Mako
...
Successfully built nbgrader sqlalchemy alembic Mako
Installing collected packages: sqlalchemy, Mako, python-editor, alembic, prompt-toolkit, ipython, 
jupyter-console, nbgrader, nbclean
...
Successfully installed Mako-1.0.7 alembic-1.0.7 ipython-6.2.1 jupyter-console-5.2.0 nbclean-0.3.2 
nbgrader-0.5.5 prompt-toolkit-1.0.15 python-editor-1.0.4 sqlalchemy-1.2.17 
Using again the jupyter-book to create catafest I got another error:
C:\Python364>jupyter-book create catafest--demo
Copying new book to: .\catafest
Copying over demo repository content
This is an error when I start to buid first time:
C:\Python364>jupyter-book build catafest
Convert and copy notebook/md files...
  0%|                                                                       | 0/35 [00:00
  File "c:\python364\lib\site-packages\jupyter_book\main.py", line 31, in main
    commands[args.command]()
  File "c:\python364\lib\site-packages\jupyter_book\build.py", line 266, in build_book
    lines = ff.readlines()
  File "c:\python364\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 183: character maps to  
The reason is the python 3.x and can be fixed by change the encoding into this file:
c:\python364\lib\site-packages\jupyter_book\build.py with this:
with open(path_new_file, 'r',encoding='utf-8') as ff:
The next step is to build the catafest jupyter-book:
C:\Python364>jupyter-book build catafest
Convert and copy notebook/md files...
... 
My GitHub account is catafest and I used this link to create a new repo named catafest_jupyter-book:
https://github.com/new
You need to use root folder use this commands:
C:\Python364\catafest>cd ..

C:\Python364>git clone https://github.com/catafest/catafest_jupyter-book
Cloning into 'catafest_jupyter-book'...
warning: You appear to have cloned an empty repository.
Copy all files and folders from catafest folder to catafest_jupyter-book folder and use GITHUB commands to upload to the web:
C:\Python364>cd catafest_jupyter-book

C:\Python364\catafest_jupyter-book>git add ./*

C:\Python364\catafest_jupyter-book>git commit -m "adding my first jupyter book!"

C:\Python364\catafest_jupyter-book>git push
Username for 'https://github.com': catafest
Password for 'https://catafest@github.com':
Enumerating objects: 347, done.
Counting objects: 100% (347/347), done.
Delta compression using up to 2 threads.
Compressing objects: 100% (304/304), done.
Writing objects: 100% (347/347), 1.40 MiB | 541.00 KiB/s, done.
Total 347 (delta 74), reused 0 (delta 0)
remote: Resolving deltas: 100% (74/74), done.
To https://github.com/catafest/catafest_jupyter-book
 * [new branch]      master -> master
You can make GITHUB settings with a new gh-pages branch to see into your browser.

Thursday, February 14, 2019

Using python with documents files.

Today I tested with python version 3.6.4 two python modules: python-docx and openpyxl.
This python modules let us to deal with document files like: docx, xlsx, xlsm, xltx, xltm.
First python module named python-docx is a Python library for creating and updating Microsoft Word (.docx) files.
The documentation of this python module can be found here.
Let's start with a simple example.
C:\Python364>cd Scripts
C:\Python364\Scripts>pip3.6.exe install python-docx
Collecting python-docx
...
Successfully installed python-docx-0.8.10
Let's start with the import step:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import docx
>>> dir(docx)
['Document', 'ImagePart', 'RT', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '
__name__', '__package__', '__path__', '__spec__', '__version__', 'api', 'blkcntnr', 'compat', 'dml',
 'document', 'enum', 'exceptions', 'image', 'opc', 'oxml', 'package', 'parts', 'section', 'settings'
, 'shape', 'shared', 'styles', 'text']
Let's create a document with this python module and add some text and an image:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import docx
>>> mydoc = docx.Document()
>>> mydoc.add_paragraph('This is a text')
>>> mydoc.add_picture('icon.png',width=docx.shared.Inches(1),height=docx.shared.Inches(1))
>>> mydoc.save('test.docx')
Another python module is openpyxl.
This python module let you to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.
C:\Python364\Scripts>pip3.6.exe install openpyxl
Collecting openpyxl
...
Successfully installed et-xmlfile-1.0.1 jdcal-1.4 openpyxl-2.6.0
Let's test it:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from openpyxl import load_workbook
>>> w = load_workbook(filename='test.xlsx',read_only=True)
>>> print(w.sheetnames)
['Sheet1', 'TestSheet2']
>>> s=w['Sheet1']
>>> for row in s.rows:
...     for c in row:
...             print(c.value)
...
A1
None
ABC
None
None
NOP

Sunday, February 10, 2019

Using the asciimatics and pyfiglet python modules

This is a simple example how to use the asciimatics and pyfiglet python modules with python version 3.6.4.
First you need to install with the pip tool.
The source code is simple and start with the import it.
The Fire, Print and Screen is used to show the fire effect and print text with Figlet and FigletText.
Because the fire and text use the console application I used the default Screen Buffer Size set to 80.
The Screen.wrapper(my_work_web) show all effects from the my_work_web.
In this area is created variables for font type: banner_font and web_font.
The main reason I named the web_font was to show my web page but the size of the over the screensize.
I tested most of the fonts from pyfiglet python module but I cannot find one to show a web page link.
This is the source code I tested:
# -*- coding: utf-8 -*-
"""
@author: catafest
"""

from asciimatics.renderers import FigletText, Fire
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.effects import Print
from asciimatics.exceptions import ResizeScreenError
from pyfiglet import Figlet
import sys

def my_work_web(screen):
    banner_font = "banner3"
    web_font = "block"
    scenes = []
    effects = [
        Print(screen,
              Fire(screen.height, 80, "*" * 70, 0.8, 60, screen.colours,
                   bg=screen.colours >= 256),
              0,
              speed=1,
              transparent=False),
        Print(screen,
              FigletText("Follow ", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              stop_frame=30),
        Print(screen,
              FigletText("me", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=30,
              stop_frame=50),
        Print(screen,
              FigletText("on web", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=50,
              stop_frame=70),
        Print(screen,
              FigletText("catafest", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=70),
    ]
    scenes.append(Scene(effects, 100))

    text = Figlet(font=web_font, width=300).renderText("bye!")
    width = max([len(x) for x in text.split("\n")])

    effects = [
        Print(screen,
              Fire(screen.height, 80, "*" * 70, 0.8, 60, screen.colours),
              0,
              speed=1,
              transparent=False),

        Print(screen,
              FigletText("bye!", web_font),
              (screen.height - 2)  // 2,
              colour=Screen.COLOUR_WHITE,
              bg=Screen.COLOUR_BLUE,
              speed=1)
    ]
    scenes.append(Scene(effects, -1))
    screen.play(scenes, stop_on_resize=True)


if __name__ == "__main__":
    while True:
        try:
            Screen.wrapper(my_work_web)
            sys.exit(0)
        except ResizeScreenError:
            pass
The result of this source code is this: