Running Streamlit inside my own executable with the "click" module

How do I start the streamlit app from within a Python function?

I have my own Click CLI app. I’d like one of the commands to start streamlit:

mycustomapp streamlit

should achieve the same as

streamlit run path/to/my/streamlit/app.py

What is the proper way to do this, and to organize my code? I would use Click for mycustomapp.

Hi @laserson,

You should be able to accomplish this using os.system. For example:

import os

[...]

@your_main.command()
@click.pass_context
def streamlit(ctx):
    os.system("streamlit run yourapp.py")

Check out this page for a simple click tutorial that shows setting up commands.

Let me know if this doesn’t work for you!

Thanks @nthmost! But where does yourapp.py live? In this case mycustomapp would be a setup.py-defined entrypoint, just like it is for streamlit.

I don’t quite understand what you’re asking, but here’s an example of how I got it working:

Folder structure:

.
├── exampleapp
│   ├── __init__.py
│   ├── myscript.py
│   └── wrapped_cli_tool.py
└── setup.py

setup.py

import platform
import setuptools
import subprocess

setuptools.setup(
    name='exampleapp',
    version='1.0.0',
    description='',
    long_description='',
    url='',
    author='',
    author_email='',
    license='',
    packages=['exampleapp'],
    install_requires=['streamlit', 'click'],
    zip_safe=False,  # install source files not egg
    entry_points={'console_scripts': [
        'exampleapp = exampleapp.wrapped_cli_tool:main'
    ]},
)

wrapped_cli_tool.py

import click
import streamlit.cli
import os

@click.group()
def main():
    pass

@main.command("streamlit")
def main_streamlit():
    dirname = os.path.dirname(__file__)
    filename = os.path.join(dirname, 'myscript.py')
    args = []
    streamlit.cli._main_run(filename, args)

if __name__ == "__main__":
    main()

myscript.py

import streamlit as st

st.write('It worked!')

Instructions

  • Call python setup.py install
  • Then try exampleapp streamlit! You should see “it worked” on your browser.

Perfect! I think the “API” call I was looking for was streamlit.cli._main_run(). Looking at the streamlit code, the hello subcommand actually implements this too. (Apologies…I should’ve looked there first.)

It also gets the path in a pretty clean way:

Thanks again!

1 Like