Forum Archive

Pythonista How do I open a URL stream?

Simdvo

Hi,

I'm doing a Project in which I want to control a Car over WIFI.
Does anyone know how to Program a Script in which there are buttons for Forward, Backwards,Left and Right. It is also important that I can watch a Stream from a camera which i mounted on top of the car.
I can open the Stream in Safari so I think it might be possible with load_url in a UI.WebView but I just cant figure out the Code.
Is there anyone that knows how to program such a thing?
This is the code I figured it out myself. But I just don't understand how to do the camera thing.
I'm a total beginner to so that doesn't makes it more simple.

from scene import *
import sound
import random
import math
import webbrowser
A = Action

class MyScene (Scene):
    def setup(self):

        self.buttonl = SpriteNode('iob:arrow_left_b_256')
        self.buttonl.position = (70, 150)
        self.buttonl.scale = 0.7
        self.add_child(self.buttonl)

        self.buttonr = SpriteNode('iob:arrow_right_b_256')
        self.buttonr.position = (300, 150)
        self.buttonr.scale = 0.7
        self.add_child(self.buttonr)

        self.buttonf = SpriteNode('iob:arrow_up_b_256')
        self.buttonf.position = (185, 300)
        self.buttonf.scale = 0.7
        self.add_child(self.buttonf)

        self.buttonh = SpriteNode('iob:arrow_down_b_256')
        self.buttonh.position = (70, 400)
        self.buttonh.scale = 0.7
        self.add_child(self.buttonh)

        self.buttons = SpriteNode('iob:close_circled_256')
        self.buttons.position = (310, 410)
        self.buttons.scale = 0.5
        self.add_child(self.buttons)

    def did_change_size(self):
        pass


    def touch_began(self, touch):

        for touch in self.touches.values():
            if touch.location in self.buttonl.bbox:
                webbrowser.open("http://192.168.1.160/links")
                print("links")
            if touch.location in self.buttonr.bbox:
                webbrowser.open("http://192.168.1.160/rechts")
                print("rechts")
            if  touch.location in self.buttonf.bbox:
                webbrowser.open("http://192.168.1.160/forne")
                print("forne")
            if  touch.location in self.buttons.bbox:
                webbrowser.open("http://192.168.1.160/stop")
                print("stop")
            if  touch.location in self.buttonh.bbox:
                webbrowser.open("http://192.168.1.160/zuruck")
                print("zurück")

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        webbrowser.open("http://192.168.1.160/stop")

if _name_ == '_main_':
    run(MyScene(), show_fps=False)
cvp

@Simdvo did you read this topic

Simdvo

I did actually but I don't know how to use the command load_url and UI.WebView.
Is it possible that you could send me a short example code so that I know how to
integrate it into my code?

cvp

@Simdvo only to show

import ui

class MyWebView(ui.View):
    def __init__(self, url):
        self.width, self.height = ui.get_window_size()
        self.wv = ui.WebView(frame=self.bounds)
        self.wv.load_url(url)
        self.add_subview(self.wv)
        bi_back = ui.ButtonItem(image=ui.Image.named('iob:ios7_arrow_back_32'))
        bi_forward = ui.ButtonItem(image=ui.Image.named('iob:ios7_arrow_forward_32'))
        bi_back.action = self.go_back
        bi_forward.action = self.go_forward
        self.right_button_items = [bi_forward, bi_back]
        self.present()

    def go_back(self, sender):
        self.wv.go_back()

    def go_forward(self, sender):
        self.wv.go_forward()

wv = MyWebView('https://forum.omz-software.com') 
cvp

But there is better than ui.WebView, see WKWebView

Simdvo

Could you send me a code for the

Simdvo

I mm sorry could you send me a code for the wkwebview to? I don't realy unterstand how to use it from the link you send

cvp

@Simdvo First, did you try my code with the url of the camera?
WKWebView has the same principle of working but is newer.
Thus, please, try ui.WebView first, if it does not work, WKWebView will not too

cvp

@Simdvo said:

I don't realy unterstand how to use it from the link you send

When you see a link pointing to GitHub, you have to download the code in your site-packages folder and then, this code can be imported like a standard code.

But anyway, in your case, this newer module is not really needed to test that you can (or not) access your camera in a WebView as you can in Safari.

Simdvo

The ui.webview funktioned. But it showed the webside in a new window that covered the whole screen. Is it possible that it is only a small window so that i can use the buttons for the movement at the same time?

mikael

@Simdvo, it is a view like any other, so you can set its frame (or x, y, width, height) as you wish.

mikael

@Simdvo, what is the car that you are driving this way? Does it have out-of-the-box support for Python?

Simdvo

It is based on a. Esp32 and reackts if a url is triggered. It is programmed with the arduino ide(C++).

Simdvo

What is the command to set the windows frame?

cvp

@Simdvo ex:

self.wv.frame = (10,10,400,400)
Simdvo

when I copy that line in the code than i mm getting the error „‘MyWebView‘ object has noattribute wv“ in that line.

cvp

@cvp of course, you have to insert this line at the right place

        self.wv = ui.WebView(frame=self.bounds)
        self.wv.frame = (10,10,400,400) 
Simdvo

If i start the programm the controllboard (buttons for forward,backward...) opens but then gets covered by the webside. The image is as big sa i set it but the background is black and coveres the whole screen.

Simdvo

from scene import *
import sound
import random
import math
import webbrowser
import ui
A = Action

class MyWebView(ui.View):
    def __init__(self, url):
        self.wv = ui.WebView(frame=self.bounds)
        self.wv.frame = (10,10,350,250) 
        self.wv.load_url(url)
        self.add_subview(self.wv)
        self.present()



class MyScene (Scene):
    def setup(self):

        self.buttonl = SpriteNode('iob:arrow_left_b_256')
        self.buttonl.position = (70, 150)
        self.buttonl.scale = 0.7
        self.add_child(self.buttonl)

        self.buttonr = SpriteNode('iob:arrow_right_b_256')
        self.buttonr.position = (300, 150)
        self.buttonr.scale = 0.7
        self.add_child(self.buttonr)

        self.buttonf = SpriteNode('iob:arrow_up_b_256')
        self.buttonf.position = (185, 300)
        self.buttonf.scale = 0.7
        self.add_child(self.buttonf)

        self.buttonh = SpriteNode('iob:arrow_down_b_256')
        self.buttonh.position = (70, 400)
        self.buttonh.scale = 0.7
        self.add_child(self.buttonh)

        self.buttons = SpriteNode('iob:close_circled_256')
        self.buttons.position = (310, 410)
        self.buttons.scale = 0.5
        self.add_child(self.buttons)

    def did_change_size(self):
        pass


    def touch_began(self, touch):

        for touch in self.touches.values():
            if touch.location in self.buttonl.bbox:
                webbrowser.open("http://192.168.1.160/links")
                print("links")
            if touch.location in self.buttonr.bbox:
                webbrowser.open("http://192.168.1.160/rechts")
                print("rechts")
            if  touch.location in self.buttonf.bbox:
                webbrowser.open("http://192.168.1.160/forne")
                print("forne")
            if  touch.location in self.buttons.bbox:
                webbrowser.open("http://192.168.1.160/stop")
                print("stop")
            if  touch.location in self.buttonh.bbox:
                webbrowser.open("http://192.168.1.160/zuruck")
                print("zurück")

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        webbrowser.open("http://192.168.1.160/stop")

if __name__ == '__main__':

    run(MyScene(), show_fps=False)
    wv = MyWebView('https://forum.omz-software.com')

Simdvo

Does somebody know how to solve the problem that i mentioned before

mikael

@Simdvo , do not present the view, but use the view that scene already has as a basis:

class MyWebView(ui.View):
    def __init__(self, url, scene):
        scene.view.add_subview(self)
        self.touch_enabled = False
        self.frame = scene.view.bounds
        self.wv = ui.WebView(flex='WH')
        self.wv.frame = (10,50,350,250) 
        self.add_subview(self.wv)
        self.wv.load_url(url)

... and providing scene in the main:

if __name__ == '__main__':
    scene = MyScene()
    run(scene, show_fps=False)
    wv = MyWebView('https://forum.omz-software.com', scene)

I also moved the webview a bit to not cover the X, and made it touch-transparent, to facilitate closing the view.

As an aside, for future reference, this would have been easier to build all in the ui module, without scene.

Simdvo

Thank you, but when I try to run the code I getting the Error:
name 'MyScene' is not defined

sam rod

In preference, the scene look runnable. The size and position is editable.

Simdvo

Does anyone know how to solve my problem?

mikael

@Simdvo, my code was just replacements for the same parts in your code, not runnable as is.

mikael

@Simdvo, here’s the full code for your convenience:

from scene import *
import sound
import random
import math
import webbrowser
import ui
A = Action

class MyWebView(ui.View):
    def __init__(self, url, scene):
        scene.view.add_subview(self)
        self.touch_enabled = False
        self.frame = scene.view.bounds
        self.wv = ui.WebView(flex='WH')
        self.wv.frame = (10,50,350,250) 
        self.add_subview(self.wv)
        self.wv.load_url(url)


class MyScene (Scene):
    def setup(self):

        self.buttonl = SpriteNode('iob:arrow_left_b_256')
        self.buttonl.position = (70, 150)
        self.buttonl.scale = 0.7
        self.add_child(self.buttonl)

        self.buttonr = SpriteNode('iob:arrow_right_b_256')
        self.buttonr.position = (300, 150)
        self.buttonr.scale = 0.7
        self.add_child(self.buttonr)

        self.buttonf = SpriteNode('iob:arrow_up_b_256')
        self.buttonf.position = (185, 300)
        self.buttonf.scale = 0.7
        self.add_child(self.buttonf)

        self.buttonh = SpriteNode('iob:arrow_down_b_256')
        self.buttonh.position = (70, 400)
        self.buttonh.scale = 0.7
        self.add_child(self.buttonh)

        self.buttons = SpriteNode('iob:close_circled_256')
        self.buttons.position = (310, 410)
        self.buttons.scale = 0.5
        self.add_child(self.buttons)

    def did_change_size(self):
        pass


    def touch_began(self, touch):

        for touch in self.touches.values():
            if touch.location in self.buttonl.bbox:
                webbrowser.open("http://192.168.1.160/links")
                print("links")
            if touch.location in self.buttonr.bbox:
                webbrowser.open("http://192.168.1.160/rechts")
                print("rechts")
            if  touch.location in self.buttonf.bbox:
                webbrowser.open("http://192.168.1.160/forne")
                print("forne")
            if  touch.location in self.buttons.bbox:
                webbrowser.open("http://192.168.1.160/stop")
                print("stop")
            if  touch.location in self.buttonh.bbox:
                webbrowser.open("http://192.168.1.160/zuruck")
                print("zurück")

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        webbrowser.open("http://192.168.1.160/stop")

if __name__ == '__main__':
    scene = MyScene()
    run(scene, show_fps=False)
    wv = MyWebView('https://forum.omz-software.com', scene) 
Simdvo

Thank you the code works perfectly. But I have one last question does someone know how to send values to an URL? (post)?

mikael

@Simdvo, do you need it for the movement commands or the camera stream?

Simdvo

Yes I want to try if it is faster than the way I'm using now.

cvp

@Simdvo something like

import json
import requests

payload1 = '...'
payload2 = '...'
payload3 = '...'
url = 'https://......'
# value1,value2,value3 are reserved words in the webhook service
payload = {"value1" : payload1, "value2" : payload2, "value3" : payload3}
headers = {'content-type': 'application/json'}
print(json.dumps(payload))
post = requests.post(url, data=json.dumps(payload), headers=headers) 
ccc

requests.post(url, data=json.dumps(payload), headers=headers)

requests.post(url, json=payload, headers=headers)

cvp

@ccc said:

requests.post(url, json=payload, headers=headers)

Thanks, I always forget that 👴🏻