Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
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?
bug -
Post a minimal one or two line script that fails. It works for me
import canvas canvas.draw_rect(50,50,50,59)
What device and OS are tou using?
-
@zencuke , just FYI. I ran the red example and it worked fine. Both for 3 and 2.7. Only difference could be I am still on the beta, not the latest App Store Version.
But the clock face demo had problems. First It's using xrange. But then after changing It am getting Unicode not defined error. I am not really good enough to sort this out. Better left for @omz or others to comment. Or maybe it gives you clue to solve it. But yes it appears there are some problems with Canvas -
ahh, it seems the canvas text functions are not py3 compat, bu this is workaroundable by
adding acanvas.unicode= bytes
line after importing canvas. -
Opened a bug report https://github.com/omz/Pythonista-Issues/issues/60
-
@JonB I intentionally didn't post an example because I just used the example in the canvas documentation. I assumed everyone had fast access to that. It comes with the app. It is probably about as fast for someone to run the example as it would be to cut and paste it from the forum. It also emphasizes that it is not my code failing. I haven't started writing canvas code because the examples fail and I don't know what to do next. If I had written any code I would have posted it. I simply opened the doc, went to canvas section, scrolled to the first example, clicked on the "open in editor" button then clicked the run icon. I did this many times before I got bored and gave up, sometimes shutting down the app or powering down in between. I ran out of variations to test. I haven't reset yet. I probably should. I reset so seldom I forget how.
I am running on iPhone6 plus running 9.3.2 (I think that is the latest) and the official (non beta) version 3.
As I said the same process/example code works on the same device running original Pythonista v. 2.1 (210000). It is sort of handy having them by side by side for A-B testing. It tends to reduce operator error.
-
@JonB I forgot to mention that I tried your slightly simpler example and it didn't work.
Side note: I wish I could figure out how to make the key pad go away. I understand that iPad users get a keypad icon but the iPhone "doesn't have enough space" for that button.
-
@Phuket2 Now I'm confused. I couldn't find anything in the example directory that uses canvas. Maybe that is why it is buggy. Everything uses scene which I guess I should do as well. Canvas just looked simpler. Also I thought clock.py was removed from the release. After reading your post I finally found a copy in the "share with original Pythonista" area but that copy works fine for me. It also doesn't use canvas. It uses scene like everything else.
-
@zencuke You can make the keyboard go away on the iPhone with a gesture – put your finger above the keyboard (in the editor's main text area or the console's text output), then swipe down.
-
@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.
-
OK now that I read one days worth of response I want to summarize.
- So far 151 people have viewed this topic. Not many responded which makes me think almost no one uses canvas.
- No one reports having the same problem.
- @JonB Doesn't have my problem but had a problem with canvas text functions in py3. He posted a workaround.
- ccc filed a bug against @JonB 's canvas text issue.
- @Phuket2 has a problem with the "clock face demo" He seems to think it uses canvas but the version I have does not use canvas and it works for me. I'm not sure his problem is related to mine. Maybe he has a different version of the clock demo?
-
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
canvas
module 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 theui
module (i.e.ui.ImageContext
) or Pillow instead.Here's a simple example of a canvas-like script using
ui.ImageContext
instead of thecanvas
module: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
ui
module actually has more advanced drawing operations thancanvas
, 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.
Thanks
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
canvas
module 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.