Outils pour utilisateurs

Outils du site


issue170:python

Ceci est une ancienne révision du document !


As many of you know, I work fairly closely with Don Rozenberg who is the author of PAGE. For over 10 years, he and I have communicated strictly via email. Until the last 30 days or so. Now we are enjoying Discord video chat (voice only (my choice)) and screen sharing. It makes our communications and collaboration tremendously productive. The immediate back-and-forth is awesome, and we get a tonne of things done, talked through, and most importantly, because it’s live, understanding is immediate. I can’t tell you how many email threads that we’ve gone through where one or the other of us doesn’t quite understand an issue without 5 or more emails back-and-forth. Given the time-zone issue, there are many times that it takes 3 or 4 days to hash out a problem or thought. Now he can see what I’m doing and I can see what he is doing. If you haven’t tried Discord and the screen share/voice session before, you owe it to yourself to find someone to try it with. It’s not the most intuitive thing in the world, but with a bit of patience, it’s easy to get up to speed quickly.

Now the reason I am mentioning this is because, on today’s update session, he asked if I had ever used PtPython. I honestly said no, and he proceeded to demo it for me and I was immediately impressed. (Note that I’m fairly easy to impress, so that’s not a big thing.) While he was showing me what it can do, I grabbed my smartphone and looked it up. I decided right then and there to use it as my article for this month.

You can find the home page for PtPython at https://github.com/prompt-toolkit/ptpython.

I have to admit that I REALLY hate the “normal” Python Interactive Shell or REPL. While it’s very useful many times to test code before I try to make it work in my IDE, I find it very clumsy especially when I’m trying to prototype a function or complex loop. More times than not, I’ll just work it up in VS Code and if it doesn’t work, I’ll set a breakpoint and step through my code until I find where I have messed up.

One of the reasons that I was so excited about PtPython is that it allows you to use history to dump an entire loop or function back into active code and make a quick edit due to a typo.

Let’s take a look at using PtPython. First, we need to install it. You can use pip to do this…

pip install ptpython

However, when I did this, I received a number of dependency errors. So, I uninstalled it, downloaded the repository as a zip file, unpacked it and then did an “old-fashioned” python install from the repository folder…

python setup.py install

Then I installed it again via pip and everything seemed to work.

The first thing you might notice is that, unlike the “normal” Python REPL, it doesn’t start with the version number. That, you can find in the bottom right of the window.

Now this is where I started to kind of “choke up”. I couldn’t for the life of me, come up with anything to try. So, I did a simple test purposefully making a mistake…

a = 3
b = 2
for cntr in range(a):

… for cntr2 in range(b): … print(f'{a} - {b}') 3 - 2 3 - 2 3 - 2 3 - 2 3 - 2 3 - 2

Instead of printing the values of cntr and cntr2, I told it to print a and b. To fix the error, I simply pressed the up arrow key. It then put in my entire loop. I then used the left arrow and replaced the “a” with “cntr” and the “b” with “cntr2”.

I then pressed {enter} twice and the output is as I wanted it. Much better. AND MUCH easier than dealing with the standard REPL.

As I got ready to do the next section below, I remembered that there is autocomplete already built into ptpython. For example… Just one more checkmark for ptpython!

Now that I’d gotten all that done, I started thinking to myself, “Self, what else can I do to show the goodies that ptpython has to offer?” And I answered myself, “Well, Self, how about showing off the PyWebIO library at the same time you show off the ptpython?”. Who am I to argue with myself, right? So, here we go.

PyWebIO

According to their website https://github.com/wang0618/PyWebIO,

“PyWebIO provides a series of imperative functions to obtain user input and output on the browser, turning the browser into a “rich text terminal”, and can be used to build simple web applications or browser-based GUI applications without the need to have knowledge of HTML and JS.”

I stumbled across this library while trying to keep up with the news about Python while trying to deal with other things. I wasn’t really sure when I was going to be able to show it to you, but as they used to say, “There’s no present like the time”. Well, THEY say it differently, but I like to be different.

So, to install it, simply use pip…

pip3 install pywebio

And you are ready to go.

Now in ptpython, do an import of the package…

from pywebio.output import *

Now, I’m going to try to recreate the terminal animation that they have on their website.

put_text(“Hello World!”);

When you do this, your default web browser should pop open and show you…

Easy enough, right? Now, let’s do something a bit fancier…

put_table([

… ['Product', 'Price'], … ['Apple', '$5.5'], … ['Banner', '$7'], … ]);

And your browser window will update to show… Those are some expensive apples, but it gets the point across. Tables are REALLY easy. Notice that it automatically made the headers bold.

We can even, easily, put a logo onto the web page…

put_image(open('FullCircleLogo.jpg','rb').read());

You can even put interactive buttons on your web page…

def on_click(btn):

… put_markdown(“You clicked '%s' button” % btn)

put_buttons(['A', 'B','C'], onclick=on_click);

Which shows three buttons, just like we asked. When you click each, this is what it will look like…

There is so much more that can be done with these two packages, that really, your imagination is your only limitation.

This month’s article will really stress Ronnie getting everything to line up, so I think it might be a good idea that I end up. (I'd rather line up images than try and line up all those usual code snippets! - Ronnie)

Until next time, as always; stay safe, healthy, positive and creative!

issue170/python.1624694593.txt.gz · Dernière modification : 2021/06/26 10:03 de auntiee