So I am creating an application that will run a python script in the background. I have the code to do this (below), but I don't know exactly how to implement it.

Ideally, the user would launch the app, click the "Start" button and it would then sent the app into the background and start the python script execution. The user would then open their browser (on a separate computer) and navigate to the ip address of the device and the listening port to access the UI.

This code makes your iOS app run indefinitely in the background. Copy and paste the below methods into a singleton / manager which handles the tasks you need to perform in the background.

The code below is within my MyApp.h and MyApp.m files. I just don't know how to execute the background task on the button click.

Code:
// @interface

// Declare Private property
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask;

//@end
// ...

// Copy into
//@implementation 

- (void)setupBackgrounding {
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appBackgrounding:)
                                                 name: UIApplicationDidEnterBackgroundNotification
                                               object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(appForegrounding:)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object: nil];
}

- (void)appBackgrounding: (NSNotification *)notification {
    [self keepAlive];
}

- (void) keepAlive {
    self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
        [self keepAlive];
    }];
}

- (void)appForegrounding: (NSNotification *)notification {
    if (self.backgroundTask != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
}