dans6887
Posts: 1
Joined: Mon Feb 18, 2019 2:57 pm

Using python scripts in html / javascript

Mon Feb 18, 2019 3:04 pm

Please help. I am building a raspberry pi robot that I want to control over a web interface. I have written several python scripts to control movement and pan / tilt functions. I have a web interface ready to go that I designed using HTML5, CSS, and Javascript. I just dont know how to incorporate the python code to run the robot. I am using a raspberry pi model b+ with the latest verion of Raspbian as the OS.

User avatar
MrYsLab
Posts: 376
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Using python scripts in html / javascript

Mon Feb 18, 2019 11:16 pm

I am not clear what you mean by " incorporate the python code ". If you mean somehow executing in HTML, you can't, however, you can use websockets. I did this for one of my projects and is shown in the get_message method of this file https://github.com/MrYsLab/pymata-aio/b ... iot.py#L72

The file is huge, but you can ignore most of it since it uses a web page to control an Arduino. You can view the matching web page here:
https://github.com/MrYsLab/pymata-aio/b ... ester.html

Just look for JSON in the html file to see the messages being assembled and sent or received.

The messages are JSON messages, but on the python side, you can view them as Python dictionaries.

If you want to see a simple interaction, here is the code for a very simple python websocket server. Start this up, bring up the webpage above and click around and you will the messages printed in the python console. Pick the second tab, UNO Digital/PWM Output, since it sends messages.

Code: Select all

import asyncio
import websockets
import sys
import json


async def hello(websocket, path):
    while True:
        data = await websocket.recv()
        print(json.loads(data))


try:
    start_server = websockets.serve(hello, '127.0.0.1', 9000)

    asyncio.get_event_loop().run_until_complete(start_server)

    asyncio.get_event_loop().run_forever()
except websockets.exceptions.ConnectionClosed:
    sys.exit()
except RuntimeError:
    sys.exit()

start_server = websockets.serve(hello, 'localhost', 9000)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Using python scripts in html / javascript

Wed Feb 20, 2019 7:18 pm

How about a pure python webapp using PySimpleGUIWeb or remi ?

User avatar
MrYsLab
Posts: 376
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Using python scripts in html / javascript

Wed Feb 20, 2019 8:21 pm

I have no experience with PySimpleGuiWeb, but have used remi successfully. It is pretty easy to use and the author provides excellent support. It has an optional drag and drop layout tool that is rather nice.

User avatar
MrYsLab
Posts: 376
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Using python scripts in html / javascript

Wed Feb 20, 2019 8:29 pm

I have used remi with the razmq project: https://github.com/MrYsLab/razmq.

You can vew the remi code here: https://github.com/MrYsLab/razmq/blob/m ... emi_gui.py

There is also a kivy GUI for that project, but I could not get it to work as a web app. It does work rather nicely as a desktop app.

I have also built a tk GUI for robot control that you can view here https://github.com/MrYsLab/xibot/blob/m ... s/xitk.png

The code is here: https://github.com/MrYsLab/xibot

Return to “Python”