Control python program with django - python

Hej,
I have written a little music player like winamp in python. It has a GUI (Songname, Play/Pause, Next, Previous, time progress bar), I can open a folder and the audio files are played one after the other.
Now I really like to add a remote control through a webserver hosted on the same machine. My idea is to have the same buttons and outputs (also a time progress bar) as in the GUI on a webpage.
Scenario: I start my music player on my computer (it also automatically starts a webserver), I choose my folder with songs and skip two songs by clicking on the GUI on my desktop. Now I decide to sit on the couch and read a book. After a while the music annoys me and I want to pause it. I grab my phone, visit the webpage and tap on the pause button. The music stops and also the desktop GUI reacts (stops the time progress bar and displays the play button instead of the pause button).
I know how to create a django server and how to run it, but I do not know how to let the django views communicate with my music player on my desktop.
I have googled and read some stuff but I have no idea which way to go:
signals: don't know if this makes any sense since the two processes are totally detached
communicate over a database, save the state of the music player in a database row and let both processes update the entry whenever a button is pressed: don't like this idea because I'd like the two processes to commuicate without interpreter
maybe use UDP/TCP: but why, the processes are on the same machine
If I had to program it right now, I'd choose the database approach or is there any other solution I haven't stumbled upon yet? Shall I use flask instead of django?
If this is a common question and task, I am sorry for repeating, but I just could not find anything stating how to solve this.

What you are looking for is IPC (inter process communication). My instinct would be to have the music player open a socket, bind to a port, and to listen for commands.
Then in the Django view you open a socket connection to the player, send the command and return a response to the browser.
I you want full two way communication to the browser then you would need to also have a process waiting for output from the music player and sending it back to the browser via a web socket connection. The standard socket tutorial is very handy.

Related

Icecast stream switching sources without the clients stopping playback

Good morning :)
I have some questions regarding an icecast-setup.
We have my own (Nextcloud)-Server sitting in our church. This works perfectly fine and since here in Germany the community services got forbidden already once during all this Covid19-stuff, we want to have a stream-setup. I managed to set up an icecast-instance on our server and we use my old laptop with Rocket Broadcaster and a Steinberg CI2 to provide the source for the stream. That works all as intended. We already used it once because we stopped public services for two weeks after one of our member got tested positive after he went abroad for a week.
Our operator on the PA doesn't want to have another display there, which would disturb him from listening the sermon.
My project regarding this: I have a RPi4 and a Behringer U-Phoria UMC202HD. The input atm is a standard mic, that is connected to the Interface.
The Pi is configured with darkice and uses its own icecast installation, while I am testing everything at home. Since I started the streaming project, after the service we switched Rocket Broadcaster to use VLC and a folder of old service recordings (mastered and in MP3-format) to provide a source to listen while we are at work or on travels. This option gets used pretty regularly and I want to keep it going.
My plan is to have a little box with a LED levelmeter, where the input level is shown. That should be done with a little python script. Also I want to add two buttons, where presets for the two setups get loaded. Button 1: kill the current source and start the darkice livestream. Button 2: kill the current source and start the playback of old recordings. Both options should have visual feedback for the operator. The raspberry has to work in headless mode without need for a VNC or SSH-connection for the normal usage.
My problem: I tried:
sudo killall darkice && /home/pi/darkice.sh
This code will get changed, because I probably have to use ices for the mp3-playback. So basically it kills darkice, starts the ices playback (for now only restarts the stream in a blink) and vice versa.
The bash-file exists and gets executed at reboot via cronjob. That works well. When I execute the above mentioned killall command, icecast continues the broadcast almost instantly, but the stream stops on the clients. Everyone needs to restart it.
Do I have an option to change the setup, so that I am able to switch between the two options without the need for everyone to restart?
My plan was to create a bash-script, where I do this all inside and execute it via GPIO-input and pythoncode.
Thanks in advance!

Controlling a robot from the web

I am trying to get an RPi3 robot to accept commands from both the Web and from a touchscreen/keyboard interface. I have a script that handles the keyboard and I am looking for that current script I have in Python to be expanded to accept real-time input from the web. As well as trying to figure out how to send the data to the script.
More detail is below but that is the basic question.
I created the robot using a raspberry pi and 3 arduinos controlling DC motors and servos to make the bot move. The program is written in Python and is run from the command line. when run it:
queries the active serial ports on the Raspberry
opens each available port
sends an integer to the receiving arduino
the arduino responds with an identifying integer so the RPI can name the port
then the script waits for user input like "Forward"
the command is translated and sent to the correct port
the robot moves
This all works perfect. Now I want to expand it. I want to keep this functionality intact and add in a web interface so I can control it from anywhere. I've tried a couple of different things with no success. I have installed apache and I am able to serve the pages with no problem, I can get the data on the page, but I can't figure out how to get the web page to send the data to the running arduino script. My issue stems from the fact that the bot control script needs to run independent of the web page. I want to still keep the same input now from the keyboard, but I also want it to accept the data from the web page. If I invoke the bot controller from the web page each time it will need to re-establish the port connections each time which takes up to 20 seconds...
I am thinking if I create a listening script I can have the website invoke the listener which will run only to receive the data from the web and pass it to the bot controller and back. But I am not sure how to do this or if this is even the best way.
I've looked at websockets, CGI/wsgi, Flask, writing a file, and JSON and I just can't seem to string it all together.
Thanks in advance and apologies if this is not in the right place or has been answered before. Also, I have not included any code as the only solid code is the bot controller. I am hoping someone with some real expertise can help me unravel this.
thanks in advance
KenV
I would say Flask is your best option. A simple example of how you could use it:
from my_robot_stuff import move_forward
#app.route('/move_forward')
def move_forward_flask():
move_forward()
return redirect('/')
Your html would then have a button that says move forward that directs to mysite.com/move_forward. flask would process it, run the code, then redirect back to the root.

PyQt cross-platform way of communicating between running applications

Hey guys/ladies of python mastery, need some help.
I'm using PyQt to create some integrated ui elements for an application written in python (appA) that runs on windows/linux/osx, I need those elements to be able to communicate with a PyQt app (appB) I have written that runs separately (same host).
What would be the best (cross-platform) approach to creating a communication link between the integrated ui elements in one app and the standalone app i have written?
I want to be able to send/receive messages from appA to appB...
For example appA launches, integrated ui elements load in a form of an input field with a 'Send' button. Upon entering data and hitting 'Send' a test is performed to check if appB is running/needs to be launched, after appB launches, data arrives at appB and appB sends a confirmation of delivery back to appA and vice versa. This needs to work with least delay and hackery on windows/linux/osx.
I was looking at DBus but that looks a little buggy for WIN, win32api is good for WIN but useless elsewhere, maybe there is a magic bullet to these things.
Any links to tutorials/sites/docs would be great or if u have some ready code :) ! whatever , thanks in advance...
If you don't want to use raw sockets, you should try zmq (zeromq). You can find a good introduction to zmq here

Python desktop software with web interface

I am building desktop software with a Python backend and a web interface. Currently, I have to start the backend, then open up the interface in the browser. If I accidentally refresh the page - then that clears everything! What I'd like to do is start the application and have a fullscreen browser window appear (using Chrome) - that shouldn't be difficult. I have two questions:
Can refresh be disabled?
Is it possible to hook into closing my program when the web UI is closed?
Update:
What I'm looking for is more like this: geckofx. A way to embed a Chrome webpage in a desktop app. Except I'm using Python rather than C#
Your first question is a dup of disable f5 and browser refresh using javascript.
For your second question, it depends on what kind of application you're building, but generally, the answer is no.
If you can rely on the JS to catch the close and, e.g., send a "quit" message to the service before closing, that's easy. However, there are plenty of cases where that won't work, because the client has no way to catch the close.
If, on the other hand, you can rely on a continuous connection between the client and the service—e.g., using a WebSocket—you can just quit when the connection goes down. But usually, you can't.
Finally, if you're writing something specifically for one platform, you may be able to use OS-level information to handle the cases that JS can't. (For example, on OS X, you can attach a listener to the default NSDistributedNotificationCenter and be notified whenever Chrome quits.) But generally, you can't rely on this, and even when you can, it may still not cover 100% of the cases.
So, you need to use the same tricks that every "real" web app uses for managing sessions. For example, the client can send a keepalive every 5 minutes, and the server can quit if it doesn't get any requests for 5 minutes. (You can still have an explicit "quit" command, you just can't rely on always receiving it.) If you want more information on ways to do this, there are probably 300 questions on SO about it.
Instead of embeding Chrome, you may embed only Webkit ( I don't know on Windows, but on Mac and Linux is easy).
Application logic seams to be on server side, and browser used only as interface. If that is the case, you may put „onbeforeunload” in body tag, and call a js function that send an ajax request to server to die.

How do I provide the ability for multiple players to connect to my MUD?

I've recently created a short and simple Multi-User Dungeon. The things I've made are the engine of the game and the actual MUD itself, so when i click on the file it you can play the MUD. The problem I have is, I don't know how to connect it so that more than one player can play. Do you connect it to a server or something? I don't know what to do as I am new to Python and MUDs. Can some help me out by giving some examples to help me out with code and how to connect and get this game up and running?
Your MUD should be the server.
First, you want to make sure that your engine can handle multiple people changing the dungeon state at the same time.
Next, take a look at how to create a server. You probably want to look at the SocketServer class, for MUDs you probably want one of the TCP subclasses.
Each user will open a connection to your server. Normally, each connection will be handled by either a process or a thread (check out the ThreadingMixin). Inside that thread will be the user interface code for a connection (read a line from the user, pass that onto the engine, print out results to the user).
Your engine will probably run in a separate thread / process and maintain the dungeon state (list of rooms, users, items).
Good luck!
The usual thing is that your MUD server code handles the socket connections and connects them to internal player objects through a command parser. You might want to look at SocketMUD; it's a bare-bones socket handling architecture intended for MUD use, and may be just what you need.

Categories

Resources