Files
Directory Structure
main.py
1 2 3 4 |
import os if __name__ == "__main__": os.system("streamlit run home.py") |
Is that it? Yes.
We will use main.py to start the Streamlit app, passing in the python file name we want to use for the home page. In this case, home.py. Speaking of home.py, let’s look at it next.
home.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# home.py # -- LIBRARY ----------------------------------------------------------------- import streamlit as st import pandas as pd import yaml import os # -- LOCAL ------------------------------------------------------------------- from library.pandas_sql_handler import PandasSqlHandler from data_handler.data_handler import DataHandler from config import sql_column_config def main(): st.set_page_config(layout="wide") st.title("Database Table Editor") st.markdown( """ Select Action and Table to Edit from menu """ ) if __name__ == "__main__": main() |
Hmmm… this is not looking too complicated. And you would be correct.
Many folks will name the home page file, app.py. I deviated a little bit from convention here, in order to help keep track of what is going. on.
Now let’s go line by line
home.py – library import
1 2 3 4 5 6 7 |
# home.py # -- LIBRARY ----------------------------------------------------------------- import streamlit as st import pandas as pd import yaml import os |
We import some standard libraries that are used in the python world, pandas, yaml, os, and of course streamlit. The appendix will contain details on how to install the packages.
Next we will import some local files that we will create below.
1 2 3 4 |
# -- LOCAL ------------------------------------------------------------------- from library.pandas_sql_handler import PandasSqlHandler from data_handler.data_handler import DataHandler from config import sql_column_config |
main.py – main()
We aliased the streamlit package as st, which is pretty common alias for the package.
1 2 3 4 5 6 7 8 |
st.set_page_config(layout="wide") st.title("Database Table Editor") st.markdown( """ Select Action and Table to Edit from menu """ ) |
In the main function, we will call three streamlit methods.
1 |
st.set_page_config(layout="wide") |
By default streamlit will limit the width of the content on the webpage. For our tutorial, I will set the layout to wide, to allow for using the complete width of the browser window.
1 |
st.title("Database Table Editor") |
Next we will add a title for the page. By default it is size of H1 html tag.
1 2 3 4 5 |
st.markdown( """ Select Action and Table to Edit from menu """ ) |
The last bit is to add some text, supporting markdown. You could also use some the st.text method to accomplish the same thing.
If you were to run the app you would see the following, without the menu on the sidebar, we will get to that part in a moment.
Leave a Reply