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 commentsBe the first to share your opinion
David Roman
an hour ago

Triggering a local Python app remotely offers convenient control. Moreover, the baby name generator for the USA simplifies finding culturally fitting names for newborns, making it a handy tool for parents.

https://ilovemybaby.org/baby-name-generator/

Asad Raza
19 hours ago

In <a href=“https://hillclimbracingapk.org/”>Hill Climb Racing MOD APK</a>, you can use premium cars to play across different tournaments for free.

swift codes com tr
12 days ago

Swift Codes Com Tr is a good source to gather all information about Swift Codes in Turkey.
https://swiftcodes.com.tr/

Show more replies