Launch Python apps locally from Excel

Consider this…You’ve just developed an awesome web app with Python and you want to share it with your colleagues. How should you go about it? What are your options?

Matt Doyle
Analytics Vidhya

--

Photo by Filiberto Santillán on Unsplash

Ideally you’d deploy your app on a cloud service (AWS, Google Cloud, Heroku, etc.) and simply direct potential users to a url. No fuss, no muss. But not always will you want your precious projects in the public domain. Or perhaps you don’t want to pay for the hosting service. Whatever the reason, you can still share your app for local use, for free.

“Keep you tools in one toolbox.”

Said no one…ever…I completely made that saying up, but it kind of holds true. As a developer you’re constantly building tools, potentially in different languages and using different programs. Your toys are scattered all over the shop. Where possible, try and keep things centralised to make life easy, not just for you, but also for your potential end users.

Consider your target audience’s level of experience. Are they all capable of interacting with the command line, activating virtual environments and opening their browser to the localhost with correct port number? And do they really want to have one more application open? Unlikely.

What about Excel? Can everyone open Excel and click one of these bad boys? Now we’re talking…

Bad boy. Image by author.

MS Excel is an absolute institution. I’d be surprised if there wasn’t a spreadsheet open on your PC as you read these very words! Admittedly, Excel is a bit long in the tooth when compared to the new and exciting programming languages available at the moment, but it is certainly the most prolific analytical tool out there.

This is where you can utilise the comfort and familiarity of a good-old excel macro button like a nice warm hug to the user. And all it takes is one line of VBA code behind the scenes.

Sub LaunchDashboard()command = Shell("C:\Users\" & LCase(Environ$("Username")) & "\Documents\Dasboard.bat", vbNormalFocus)End Sub

The above VBA code retrieves the Windows username to dynamically find the correct path to the batch file and runs it in the command prompt. “vbNormalFocus” leaves the command prompt open (as opposed to hidden) for visual feedback.

Now lets take a look at the actual batch (.bat) file used to open the command prompt, activate a virtual environment and then launch your dashboard in Chrome.

start http://127.0.0.1:8050/
call C:\Users\%USERNAME%\anaconda3\Scripts\activate
call conda activate dashboards
python "C:\Users\%USERNAME%\Documents\dashboard.py"
cmd /k
  • start http://127.0.0.1:8050launches default web browser at the localhost address and correct port (8050 in this case) to view the app.
  • call C:\Users\%USERNAME%\anaconda3\Scripts\activate — effectively turns the command prompt into an Anaconda prompt to be able to recognise “conda” commands. Note the use of %USERNAME% to dynamically get the Windows username.
  • call conda activate dashboards — activates the virtual environment.
  • python “C:\Users\%USERNAME%\Documents\dashboard.py” — runs the app with the dynamic file path.
  • cmd /k— keeps the command prompt window open for visual feedback.

Below is an example of the batch file being executed by the VBA code and successfully launching a dashboard on the localhost.

Successful execution of batch file and dashboard launch. Image the author.

The caveat to this example is that everyone you share your project with will need to maintain it in the same relative directory — so keep that file path simple. Users will also need to set up a virtual environment the same as yours. For the sake of brevity, I won’t go into that here, there are plenty of articles out there to guide you through that if needed, but trust me — it’s a cinch. In my opinion these minor details are more than compensated for by the practicality of “keeping your tools in one toolbox” and accessibility for everyone.

Give it a go and let me know your thoughts!

--

--

Matt Doyle
Analytics Vidhya

Curious programmer. Experienced in financial markets. Fan of a good dashboard!