Control python class remotely
Question: I made python class that controls my sound system in my house, the class looks
like that:

    
    
    from django.db import models
    import youtube_dl, pygame, glob
    
    class PlayerControl(object):
        def __init__(self):
            pygame.mixer.init()
        def download_music(self, video_url):
            ydl_opts = {
                'format': 'bestaudio/best',
                'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
                }],
                'forcefilename': True,
            }
            ydl = youtube_dl.YoutubeDL(ydl_opts);
            ydl.download(video_url)
        def get_mp3_files_list(self):
            return glob.glob("*.mp3")
        def play_music(self, fileName):
            pygame.mixer.music.load("1.mp3")
            pygame.mixer.music.play()
        def pause(self):
            pygame.mixer.music.pause()
        def replay(self):
            pygame.mixer.music.play()
        def get_volume(self):
            return pygame.mixer.music.get_volume()
        def set_volume(self, volume_to_set):
            pygame.mixer.music.set_volume(volume_to_set)
    

To take a step forward, I wanted to make kind of an interface the controls
this class remotely, using a better interface compared to python shell...

I guess that controlling it via a browser would be the simplest thing to do,
My question is - how to I modify python classes from a browser, using HTTP
requests.


Answer: If you want to control this over http, you'll need to take what you have and
build a web-accessible API. The standard industry practice for this at the
moment wild be a REST API, but for your first attempt I wouldn't get caught up
in the details.

I would recommend that you start with a micro framework such as Bottle or
Flask. The good news is that you have already written the model you want to
interact with, now you simply need to hook it up to something.

Following the MVC pattern, you will want to write a View layer which takes
information from your model, or from the return value of its methods, and
displays it in a way you wish to consume. Then write a controller using your
web micro framework of choice, which binds the model and view together such
that when you make a http request on a given endpoint it calls the relevant
methods and returns the relevant data.

I've kept everything at a high level here, but would be happy to answer
specific questions from comments.

