# Making a Streamlit Machine Learning App into a SystemD Service for Deployment via Ansible

DevFeed: [Making a Streamlit Machine Learning App into a SystemD Service for Deployment via Ansible](<https://devfeed.tech/articles/making-a-streamlit-machine-learning-app-into-a-systemd-service-for-deployment-via-ansible-28226.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/python/2019/11/13/making-a-streamlit-machine-learning-app-into-a-systemd-service.html>)

Author: Fuzzygroup

Published: 2019-11-13T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Streamlit](<https://devfeed.tech/topics/streamlit.md>), [systemd](<https://devfeed.tech/topics/systemd.md>), [Ansible](<https://devfeed.tech/topics/ansible.md>), [Deployment](<https://devfeed.tech/topics/deployment.md>), [Python](<https://devfeed.tech/topics/python.md>), [Shell](<https://devfeed.tech/topics/shell.md>), [Ubuntu](<https://devfeed.tech/topics/ubuntu.md>)

Tags: [ansible](<https://devfeed.tech/tags/ansible.md>), [deployment](<https://devfeed.tech/tags/deployment.md>), [git](<https://devfeed.tech/tags/git.md>), [linux](<https://devfeed.tech/tags/linux.md>), [python](<https://devfeed.tech/tags/python.md>), [shell](<https://devfeed.tech/tags/shell.md>), [streamlit](<https://devfeed.tech/tags/streamlit.md>), [systemd](<https://devfeed.tech/tags/systemd.md>), [ubuntu](<https://devfeed.tech/tags/ubuntu.md>)

## AI overview

This tutorial describes deploying a Streamlit machine learning application written in Python on an Ubuntu server. It presents Ansible automation and recommends replacing nohup and manual process management with a systemd service that can be started, stopped, and monitored with systemctl.

## Source excerpt

As I documented recently, I've become quite the fan of Streamlit for taking Machine Learning applications written in Python and making them easily available on the web. Streamlit is a very cool toolkit for taking a Python Machine Learning app and adding a simple UI to it and then hosting it via a built in web server. I haven't seen anything really like in the Ruby world and I'm quite impressed by its elegance and simplicity. Note: Before you start down the path of implementing this the way I did, see the last section titled "Note: " after the Jenkins section. So we have a custom internal app using Streamlit and I am, at present, the only person who can deploy it. That's, well, stupid. Deployment always needs to be something that anyone on the technical team should be able to do. This became increasingly obvious to me when I had to pull over, on Monday afternoon, and deploy fixes to it - in the first snow of the season. And, yes, I've been a consultant for a lot of my career so I'm ok with this but it still is sub optimal. Here's what a deploy using Streamlit onto an Ubuntu server looks like: SSH into the box. Change into the right directory. Do a ps auwwx grep streamlit and grab the pid (process id). Do a kill pid. Do a git pull. Source the python virtual environment with: source ./venv/bin/active Restart it with the syntax: nohup streamlit run dashboard.py All of this should be able to be automated with Ansible. Well, let's make that most of it. The flaw in the ointment is the last thing. Apparently the only thing that Ansible can't automate is something with nohup. Now I'm sure if I dug in enough I could either understand it find a way around it but a seemingly solid Stack Overflow post argues that the right approach is to use SystemD and services rather than nohup and that seems like a good idea actually. Ansible can easily start and stop SystemD services so we can throw out the entire pid / kill stuff. A Shell Script and a System D Unit File I don't claim to be