RSS 2016-02 - 1/1 Blog automation (4) documentation (8) example (2) latex (2) notebook (4) sphinx (10)


2016-02 - 1/1

Share buttons on a page from the documenation.

2016-02-14

pyquickhelper now includes a directive and a role to share a page from the documentation on networks. See ShareNetDirective and sharenet_role. The syntax is the following:

:sharenet:`twitter-facebook-linkedin-20`

The text must be different networks separated by -. The last integer indicates the size of the circles. If the buttons do not show up on the page, try to add -body:

:sharenet:`twitter-facebook-linkedin-20-body`

It specifies the javacript which draws the buttons is included on the HTML body and not in the header. The header section might be rewritten by other custom commands such as autodoc. The main javascript part is the following:

<script>
function share_url(share) {
    var url = share + encodeURIComponent(window.location.href);
    window.location.href = url;
}

function share_icon(divid, text) {
    var canvas = document.getElementById(divid);
    var context = canvas.getContext('2d');
    var centerX = canvas.width / 2;
    var centerY = canvas.height / 2;
    var radius = centerX;

    context.beginPath();
    context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
    context.fillStyle = '#444444';
    context.fill();
    context.font = '' + (centerX*4/3) + 'pt Calibri';
    context.textAlign = 'center';
    context.fillStyle = '#FFFFFF';
    context.fillText(text, centerX, centerY+centerY*16/30);
}
</script>

Then to add a button to share on facebook:

<a href="#" onclick="share_url('https://www.facebook.com/sharer/sharer.php?u=');return false;"><canvas height="20" id="canvas-f" width="20"/></a>
<script>share_icon('canvas-f', 'f');</script>

On Linkedin:

<a href="#" onclick="share_url('https://www.linkedin.com/shareArticle?mini=true&amp;title=&amp;summary=&amp;source=&amp;url=');return false;"><canvas height="20" id="canvas-in" width="20"/></a>
<script>share_icon('canvas-in', 'in');</script>

On twitter:

<a href="#" onclick="share_url('https://twitter.com/home?status=');return false;"><canvas height="20" id="canvas-t" width="20"/></a>
<script>share_icon('canvas-t', 't');</script>

post

Bigger text in the documentation

2016-02-14

pyquickhelper now includes a role bigger which make a text bigger

:bigger:`bigger`

Or smaller

:bigger:`::1:smaller`

Or even bigger

:bigger:`::6:even bigger`

post

read_csv and zip files

2016-02-06

read_csv is no longer able to extract a dataframe from a zip file. The parameter format changed for compression but the zip format disappeared from the list. I assume the reason is that zip files can contains many files.

pyquickhelper now implements the function read_csv which can extract all dataframe in a zip file or falls back into the regular function if no zip format is detected. In that case, it returns a dictionary of dataframes indexed by their name in the zip file.

from pyquickhelper.pandashelper import read_csv
dfs = read_csv("url_or_filename.zip", compression="zip")
print(dfs["dataframe.txt"].head())

If only one file must be converted as a dataframe, the parameter fvalid must be used:

from pyquickhelper.pandashelper import read_csv
dfs = read_csv("url_or_filename.zip", compression="zip",
               fvalid=lambda name: name == "the_file.txt")
print(dfs["the_file.txt"].head())

The others files will be loaded as text. In more details, when it is a zip file, the function reads a dataframe from a zip file by doing:

import io, zipfile, pandas

def read_zip(local_file, encoding="utf8"):

    with open(local_file, "rb") as local_file:
        content = local_file.read()

    dfs = {}
    with zipfile.ZipFile(io.BytesIO(content)) as myzip:
        infos = myzip.infolist()

        for info in infos:
            name = info.filename
            with myzip.open(name, "r") as f:
                text = f.read()

            text = text.decode(encoding="utf8")
            st = io.StringIO(text)
            df = pandas.read_csv(st, compression=compression, **params)
            dfs[name] = df

    return dfs

post


RSS 2016-02 - 1/1 2015-04 (8) 2015-05 (4) 2015-08 (2) 2015-10 (1) 2015-12 (3) 2016-01 (1) 2016-02 (3) 2016-04 (1) 2016-06 (1)