By: Aurellia Christie · May 30, 2024
sectorscan
in your chosen working directory and create a file called sectorscan.py
inside it.
streamlit
in your local. If you’ve done that, now import it to your Python script (sectorscan.py
) using this code:
CTRL + C
in your command line.
.streamlit
in your sectorscan
folder, and fill it with a file named secrets.toml
.
SECTORS_KEY
and paste your API key there:
st.secrets
:
raise Exception
. Since we’re creating an app, we need to let the user knows if something went wrong, but in the same time, not being too specific. Thus, we’ll use st.error()
to notice the user about the error:
st.cache_data
decorator to our function to make use of cache on our function calls. Cache is a mechanism for temporarily storing frequently accessed data to improve performance and reduce redundant computations.
So the final function looks like this:
st.write()
:
Always rerun
button on the top right of the page. But if you only want to reload the app when you ask the app to do so, click the Rerun
button instead. If the function works, your app should look like this after you click the button (I’ve removed the welcome text to make the app tidier):
st.write()
and let’s continue to bring more content to our app!
st.multiselect()
for this:
options
variable to contain the selected sectors to be used across our code later. And to ensure the visualizations remain clear and easy to read, we limit the maximum selections to 5 sectors. When your app reloads, it should show the multiselect filter as follows:
..-..
format, so we’ll utilize format_func
parameter of the st.multiselect()
to display the options in a prettier way:
st.multiselect()
, there’s no option to disable the clear options
button, thus, we have to make sure that we handle the case when user clears the options (no sector is selected). We’ll use st.warning()
to warn the user about this:
sectorscan.py
. Copy paste the last chunk of code of this section.
Since now we’re retrieving the data based on the selected sectors, we need to modify the code a bit. Instead of using range(3)
, we’ll change it to our options
variable:
sectorscan.py
:
df_mc_curr
and the mc_curr_chart
code in this sectiondf_mc_hist
and the mc_hist_chart
code in this section.st.columns()
. And to display the altair
charts, we’ll use st.altair_chart()
:
df_mc_change
and the code of mc_change_chart
from this section to sectorscan.py
.
Since the second row is not divided into columns, we can directly use st.altair_chart()
to display the chart:
range(3)
to options
:
st.selectbox()
to do so:
option
variable will contain the selected valuation metric and we’ll use it to make our valuation_chart
to be dynamic. Move the valuation_chart
code from this section to sectorscan.py
, but change all the Price/Book Ratio
to option
. Then, display it using st.altair_chart()
:
Market Cap
to display Top companies based on Market CapGrowth
to display Top companies based on Revenue GrowthProfit
to display Top companies based on ProfitRevenue
to display Top companies based on Revenuesectorscan.py
. Also change the range(3)
to options
:
sectorscan.py
:
df_top_mc
and the mc_chart
code in this sectiondf_top_growth
and the growth_chart
code in this sectiondf_top_profit
and the profit_chart
code in this sectiondf_top_revenue
and the revenue_chart
code in this section.st.tabs()
:
New
button:
Create repository
button.
sectorscan.py
that we’ve created throughout this recipe.
requirements.txt
with the content as follows:
Add file
- Upload files
button on the repository page:
Commit changes
button. Your repository is ready to be deployed! Your repository page now should look like this:
Create app
button on the top right:
Yup, I have an app
option.
sectorscan.streamlit.app
.
Advance settings...
link below the App URL
and paste the content of secrets.toml
in there:
Save
button.
Deploy!
button and congrats, your app is ready!