What's with canvas in Pythonista 3.
I want to draw some simple mostly path based things so I decided to try canvas, For one reason or another I've never used it. I started with the first example (red circle) clicking on "open in editor" and running. Nothing visible happens. I know it is running because I can add print statements and they show. No errors. Just no graphics. I switch to Pythonista 2 and sure enough the example prints a big red circle as promised. It is agnostic. It fails on both 2,.7 and 3.5. I power cycled and tried again. Still nothing. Is this a known Pythonista 3 problem or is it just me?
OK new information. I mislead people because I screwed up changing the python version. I was hitting the version icon in the console not realizing that only changed the version running in the console. When I used the correct methodology and changed the default versions in "file viewer settings" to 3.5 canvas code demo works. On my system the failure "silent (no errors) missing canvas output" only happens in 2.7. There are probably other issues though. After I run the canvas red circle example (in 3.5) I can't clear the console with the clear button. Actually (more experimentation) I can clear normal console text output but the canvas layer stays. Text output is masked by content in canvas layer. Wait... Typing canvas.clear() in the console does clear it. Maybe that's actually a feature and I just missed in the documentation?
The clock demo bug @Phuket2 reported also fails when I correctly set the version to 3.5.
Could someone try the canvas demo in 2.7 please to see if that failure is repeatable.
@Phuket2 I just changed xrange to range in my version and the clock demo worked for me. I don't think this is a Pythonista bug. The demo needs to change if it wants to be portable between python versions. xrange is in the list of P3 backwards incompatibilities. The translate tool would probably have fixed this. There is only one range function in p3 called range which basically does what the old P2 xrange did and there is no longer anything called xrange. xrange was a speedup but even in p2.7 between range(12) and xrange(12) the difference would be hard to measure. It was really written for very big ranges.
It is interestingly difficult to say the change briefly and clearly. "xrange is still there but it is called range and xrange is gone." "Range is gone but it has been replace by xrange which is now called range."
You are correct that almost no one uses canvas. Use ui.Path drawing to image contexts or ui.ImageViews, and present in a panel. Canvas is nice for real time poking around, sunce you don't have to switch back and forth, but for anything else, use the corresponding scene or ui methods.
by the way, the clock face demo comes from the docs. there are several examples that you cen press Open In Editor to load.
To be honest, I kinda consider the
canvasmodule as deprecated. I've dragged it along for a while because I don't really have an equivalent replacement in terms of simplicity, but I would generally recommend using either the
ui.ImageContext) or Pillow instead.
Here's a simple example of a canvas-like script using
ui.ImageContextinstead of the
import ui with ui.ImageContext(512, 512) as ctx: circle = ui.Path.oval(100, 100, 200, 200) ui.set_color('red') circle.fill() img = ctx.get_image() img.show()
This one's pretty simple (just draws a red circle), but the
uimodule actually has more advanced drawing operations than
canvas, including drop shadows and clipping masks. The other big advantage is that you can do more things with your finished drawing, e.g. save it to a file, integrate it in a GUI, etc.
@omz I'm happy with that. I guessed as much when so few people responded. I was already looking for other examples. The ability to save to an image file would be useful.
I figured you would want to know about it. If it doesn't work you should probably either fix it or delete it. I'm not invested and am already switching. As an interim step you could take it out of the documentation or mark it officially deprecated and unsupported. That would have saved me time. I haven.t done much testing but I don't think canvas works at all in Python 2.7. I suspect that it actually works at some level (there are no error messages) but that you can't see it because there is a problem displaying the canvas layer. Simple canvas tests work in 3.5 but there is the text problem @JonB found.
I just want to look at some very simple geometry. Right now I want to look at some polygons my software generates. Looking at the drawing is the point. Think of it as a viewer displaying artwork. Drop shadows and the like would degrade the "view the artwork" experience. In the bad old days of DOS I would just write pixels directly into the frame buffer. I spend months drawing Mandelbrot sets and other fractals one pixel at a time, not even vectors, directly into VGA memory. When I needed vectors I invented the Bresenham without realizing it needed inventing. Before that I wrote a graphics package on an embedded platform driving a fancy spectrometer. The package drove a stepper motor driven plotter. Again I had direct control of pixels. There was no OS to get in the way.
Windows is great with sophisticated multi-threaded multimedia type stuff but the overhead to write a program to just display a simple drawing is staggering. I miss having access to the graphic card frame buffer. Fortunately in python you solve that for me. ;-) I just need to extract the minimal functionality I need from the massive documentation and then figure out how to get it on the display in all my different platforms.
I really should try to do it in as portable way as I can to ease my multi platform problem. That probably means doing the core work in pillow images (ui is obviously custom) with a simple platform specific wrapper to display the result. On Windows and Mac I'm currently using Processing in python mode which is its own custom world. It has its own non standard graphics environment and probably doesn't support pillow. Processing will never be standard python so I'll probably move my desktop apps away from Processing to direct Python scripts that use Tk or Qt for portability but stick with pillow for the portable core.
you might also look at matplotlib as another portable answer. You can manipulate pixels directly, if you like, using numpy arrays, then matplotlib.image.imsave to save it, or you can show it using imshow and show(). Or, you can use the various pyplot commands for doing line drawing without resorting to pixel bashing. A little slow starting up on ios, but otherwise works pretty well.
@zencuke Just to be clear, I do appreciate your feedback about things that are clearly broken of course, and I intend to fix them, it's just unlikely that the
canvasmodule will gain a lot of new functionality in the future.
@zencuke , this is a very nice demo using canvas with @omz turtle implementation. Sorry, I can't remember where I found it. But it's nice. I had saved it a few days ago. I changed a few settings though. Just can't remember where I found it.
This is just a copy that that I downloaded, as I say I changed a few things, but not sure what. @omz maybe you can share your original link.
@omz Works for me. ;-) The nice thing about Pythonista is that I have so many choices. Thanks for that.
turtle.py is (undocumented as far as i can tell) included in pythonista, at least as far back as 1.5
@JonB Not quite. I actually built a version of the
turtlemodule specifically for Pythonista 3, just a few months ago, and it is only available in Python 3 mode at the moment... The standard library
turtlemodule is built on top of
Tkinter, so I basically had to re-implement it from scratch, using
ui-based drawing. What I've got so far is included in Pythonista 3, but a few things are missing, and I haven't documented it (or mentioned it in the release notes) because I'm not completely sure if I want to continue with this approach.
Hello, I wanted to check whether the old canvas library had ever been fixed or whether it is now terminally deprecated. Just found out last night that all my old graphics examples don't work anymore (neither in Pythonista 2 nor in 3) and reduced it to this crashing. After some iterations (aproximately 20 out loops) there is no further output and after s hort while a full abort of Pythonista follows.
# coding: utf-8 import canvas import colorsys from six.moves import range #this crashes Pythonista 2 and 3. canvas depracated? w=h=512 canvas.set_size(w,h) def plot(): canvas.clear() for x in range(1,512,2): for y in range(1,512,2): canvas.set_fill_color(.75,.25,.25) canvas.fill_ellipse(x,y,1,1)
I think you might find this will make your code work, at least it did for me:
Unless you are trying to see the individual dots appear one at a time...
thanks for the hint.
Yes, wrapping everything in begin_update/end_update works for me as well.
Unfortunately, I was actually trying to make the "live update" visible, i.e. seeing every dot appear.
If that's not required it's straight forward to use the UI module instead, but live updates are actually relatively complex to do in there. I use this for educational purposes, so having a lot of (object oriented) code around this is quite counter-productive.
If I wrap every fill_ellipse call into begin_update/end_update (just for fun) i get exactly the same effect as without wrapping. Seems canvas doesn't like to be overloaded with update requests.
@omz Thank you. That will be helpful. I experimented with several gestures but didn't hit on that one. I kept trying to pull it down by grabbing the top edge. Are the supported gestures documented anywhere? There are a few documented in the "Using Pythonista" section. I sometimes have this paranoid feeling that everyone else gets much more from Pythonista because they know all these cool features I never heard of. I need a 4 year old to beat on my iPhone. They try everything and seem to find new gestures faster than anyone else.
BTW: I haven't told you this lately but Pythonista is the absolutely the coolest app I have ever seen. I tell everyone who will listen how great it is. My next project is to sell my kids on it who are both learning python in other contexts. And the best part is you keep adding cool new features. I was glad that I was finally able to give you a little money by getting Pythonista 3. I would have happily paid ten times that amount.
zencuke i've been playing around with python a bit the last few months on mac. i've been programming for years. i'm new to pythonista. on the surface it would seem to me that it is basically just the same standard python but on an iOS device with perhaps some interfaces to some of the other apps / services on the device. not that this isn't cool. it is but because i'm new here and your enthusiasm is so high, i'm wondering if you might be more explicit and say specifically what is that is cool so i can personally see the light much quicker vs other contexts i'm currently using and cut to the chase to some functionality i'm probably easily overlooking? :)
btw, i don't see a pythonista app for the mac in Apps. so my guess is this is only an iOS thing right?
Pythonista is iOS only. The feature are described at http://omz-software.com/pythonista/ and http://omz-software.com/pythonista/docs/ios/ The modules like ui, scene, and canvas allow you to build iOS apps in Python. The inclusion of NumPy, Matplotlib, Requests, BeautifulSoup, Flask, Jinja2, Pillow, etc. are also quite powerful.