Forum Archive

Load Image from URL

p_atrick

I am making a dumb little program to learn how to use Pythonista. I am trying to something that changes the text/image when you tap the forward or back buttons. I cannot get the image to view. Does anybody see what I am doing wrong? Thanks.

import ui

page_count = 1

# text for each page
page1_text = 'This app introduces the game of baseball.\nUse the navigtion arrows to change pages.'
page2_text = 'Page 2'
page3_text = 'Page 3'

# images for each page
page1_img = 'http://www.talmageboston.com/files/2016/04/Baseball-Grass.jpg'

def update_page(page_text, page_img):
    if page_count == 1:
        page_text.text = page1_text
        page_img.load_from_url(page1_img)

v = ui.load_view()
page_text = v['textview1']
page_img = v['imageview1']
v.present('sheet')

while True:
    update_page(page_text, page_img)
cvp

Load_view assumes you have created an UI file with your ui.view but you need to define the path/file name of your file in load_view(file).

Example: you have a file test.pyui, thus load_view('test')

And If you comment/remove the "while true" line, you'll see your picture.

p_atrick

The documentation said that by leaving it blank it will default to the same file name but with 'pyui'. The UI portion will load, and the text appears. However, the imageview is blank. I just took what worked with the text (page_text = v['textview1']) and tried it for the image. I assume the problem is there, but I am not sure what it is.

cvp

You're right for the default name.
I've tried without the "while true" and the picture is displayed

The load_view is asynchronous and you infinite loop doesn't let enough time for loading the URL.

Why do you put this while loop?

p_atrick

Not sure. I commented out the line, but it still does not load for me.

cvp

Strange, for me, it's ok
Did you move the next line left?

cvp

Does the textview object not hide the imageview object?

cvp

see

p_atrick

Here is the UI file looks like:
UI
The imageview is above the textview.

Here is what my output looks like:
output

cvp

I just got the same problem, picture does not appear any more.
Remove Pythonista app from tasks list (4/5 fingers up, slide app up) and restart Pythonista

p_atrick

It works, thanks. Do you know why I needed to restart the app?

cvp

No, sorry but @omz is watching you 😅
You'll receive an explanation.
Perhaps, the site where you get your picture does not respond immediately if you make too much requests from an app, I have had this kind of problems on Google site.

In reality, when your picture is not displayed, the script seems locked because even the close button does not response.