Codementor Events

Trigger Local Python App Remotely

Published Dec 07, 2019Last updated Jun 03, 2020
Trigger Local Python App Remotely

With an old Mac I have lying around at home and free web-based services, I’ve setup a simple app that fetches some data from an external service (YNAB) in order to run some daily budget calculations that I used to calculate manually for a long time. The output of my app is then sent back to my phone within seconds so I can trigger it from anywhere. I wanted to share the approach I’m using which has cost me nothing.

This (obviously) isn’t an approach that should be used for large scale applications or anything other than pet projects. I just wanted to highlight how simple it can be using existing free tools. There are plenty of low cost, production ready, and scalable options out there (like AWS Lambda) if you prefer to start with that approach.

My app is written in Python and served via Flask to a local endpoint (http://localhost:5000) which ngrok points to. I then have a IFTTT webhook hitting the Ngrok URL after clicking an IFTTT button widget from my phone. The app ends up broadcasting the output to my Slack account so I end up getting a push notification on my phone containing the app output within seconds of hitting the button:

Slack notification.png

Again, all free tools (Flask, ngrok, IFTTT, and Slack) and I’m using a nine year old Mac that I only use for light web browsing, streaming, and basic programming projects. So I was able to put existing equipment to use.

Brief diagram of the entire flow:

App flow diagram.png

IFTTT Button Widget

Here is the button I press on my iPhone’s home screen to trigger the app:

IFTTT button widget.jpg

On iOS, the IFTTT button widget is really simple to configure. I’ve just inserted the ngrok URL that is configured on my local computer to trigger the app:

IFTTT webhook config.jpg

Ngrok

On my Mac I enable ngrok by running:

./ngrok http 5000

This provides me the URL that I need to use with IFTTT. Just keep in mind that unless you are on a paid ngrok plan, every time you restart your computer (or just quit and re-start ngrok) you will have a new URL to paste into IFTTT. That can be an annoying thing after a while, but the key here is free.

Flask

Running Flask is as simple as specifying the local endpoint that you want to trigger your app:

@app.route('/ynab-balances')
def run():
  runYnabAccountBalanceCheck()
  return "Done"

Then in Terminal you can start the app so it’s waiting for requests:

$ python3 my_ynab_balances.py
 * Serving Flask app "my_ynab_balances" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Slack

My Python script uses a Slack API client to broadcast anything I want to my Slack account:

def send_to_slack(self, output):
  self.slackClient.chat_postMessage(channel='@Matt', text=output)

I’ve set it up to send the app output to my own Slack username which ends up like a private message to myself.

Deploy New Code Changes

When I make updates to the app, I push to Github and then I manually run git pull on my Mac to fetch the latest changes. This requires me to either physically be in front of my Mac or use a VNC app (such as Screens) to remote control my Mac. In either case it’s tedious. My next plan is to set up a simple cronjob that pulls down the latest code on a recurring basis.

Discover and read more posts from Matt Thommes
get started
post comments147Replies
apk drug
5 months ago

Step 1: Download ICAL CapCut Templates
Once you have downloaded the templates, save these files where you can easily access them. Open the CapCut application on your device. If you haven’t installed it yet, you can do it from the Google Play Store for Android or the App Store for iOS.

https://icalcapcuttemplate.com/

apk drug
5 months ago

Professional quality: With professionally designed templates, you can change your content game with high-quality visuals and effects. With captivating transitions, trendy effects, and filters, your Insta reel will groove the beat to stand out in the social crowd.
https://icalcapcuttemplate.com/loading-capcut-template/

Harry ruth2
6 months ago

The game features stunning graphics, realistic sound effects, and a variety of motorcycles, each with distinct characteristics.https://trafficriderr.org/

Show more replies