0% found this document useful (0 votes)
201 views10 pages

Build and Deploy Full Web Applications Quickly With Python - by Abdishakur - Spatial Data Science - Nov, 2020 - Medium

This document describes how to build and deploy a full-stack web application in Python using Streamlit. It involves geocoding address data from an uploaded CSV file. The user can select address columns, view geocoded results on a map, and download the output CSV. Python libraries used include Streamlit, Pandas, GeoPandas, and Plotly Express. The application is run locally with Streamlit and deployed to the web with a single command.

Uploaded by

Dirga Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views10 pages

Build and Deploy Full Web Applications Quickly With Python - by Abdishakur - Spatial Data Science - Nov, 2020 - Medium

This document describes how to build and deploy a full-stack web application in Python using Streamlit. It involves geocoding address data from an uploaded CSV file. The user can select address columns, view geocoded results on a map, and download the output CSV. Python libraries used include Streamlit, Pandas, GeoPandas, and Plotly Express. The application is run locally with Streamlit and deployed to the web with a single command.

Uploaded by

Dirga Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

1 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

import time
import base64
import streamlit as st

import pandas as pd
import geopandas as gpd
import geopy

from geopy.geocoders import Nominatim


from geopy.extra.rate_limiter import RateLimiter

import plotly_express as px

st.title() st.markdown()

st.title(“Geocoding Application in Python”)

st.markdown( “Uppload a CSV File with address columns (Street name &

2 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

number, Postcode, City)”)

streamlit run app.py

3 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

1 def main():
2 file = st.file_uploader("Choose a file")
3 if file is not None:
4 file.seek(0)
5 df = pd.read_csv(file, low_memory=False)
6 with st.spinner("Reading CSV File..."):
7 time.sleep(5)
8 st.success("Done!")
9 st.write(df.head())
10 st.write(df.shape)
11
12 cols = df.columns.tolist()
13
14 st.subheader("Choose Address Columns from the Sidebar")
15 st.info("Example correct address: Karlaplan 13,115 20,STOCKHOLM, Sweden")
16
17 if st.checkbox("Address Formatted correctly (Example Above)"):
18 df_address = choose_geocode_column(df)
19 st.write(df_address["geocode_col"].head())
20 geocoded_df = geocode(df_address)
21 display_results(geocoded_df)
22
23 if st.checkbox("Not Correctly Formatted"):
24 df_address = create_address_col(df)
25 st.write(df_address["geocode_col"].head())
26 geocoded_df = geocode(df_address)
27 display_results(geocoded_df)
28
29
30 if __name__ == "__main__":
31 main()

main_function.py hosted with ᤹


᤻ by GitHub
᤺ view raw

file_uploader()

4 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

choose_geocode_column()

create_address_col()

geocode() display_results()

1 def create_address_col(df):
2 st.sidebar.title("Select Address columns")
3 st.sidebar.info(
4 "You need to select address column (Street name and number), post code and City"
5     )
6 address_name = st.sidebar.selectbox("Select Address column", df.columns.tolist())
7 post_code = st.sidebar.selectbox("Select Post Code Column", df.columns.tolist())
8 city = st.sidebar.selectbox("Select the City Column", df.columns.tolist())
9 country = st.sidebar.text_input("Write the country of the addresses")
10 df["geocode_col"] = (
11 df[address_name].astype(str)
12 + ","
13 + df[post_code]
14 + ","
15 + df[city]
16 + ","
17 + country
18     )
19 return df
20
21
22 def choose_geocode_column(df):
23 selection = st.selectbox("Select the column", df.columns.tolist())
24 df["geocde_col"] = df[selection]
25 return df

columns.py hosted with ᤹


᤻ by GitHub
᤺ view raw

5 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

create_address_col()

choose_geocode_column()

geocode()

1 def geocode(df):
2 locator = Nominatim(user_agent="myGeocoder")
3 geocode = RateLimiter(locator.geocode, min_delay_seconds=1)
4 df["location"] = df["geocode_col"].apply(geocode)
5 df["point"] = df["location"].apply(lambda loc: tuple(loc.point) if loc else None)
6 df[["latitude", "longitude", "altitude"]] = pd.DataFrame(
7 df["point"].tolist(), index=df.index
8     )
9 return df

geocode.py hosted with ᤹


᤻ by GitHub
᤺ view raw

6 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

1 @st.cache(persist=True, suppress_st_warning=True)
2 def display_map(df):
3 px.set_mapbox_access_token(
4 "pk.eyJ1Ijoic2hha2Fzb20iLCJhIjoiY2plMWg1NGFpMXZ5NjJxbjhlM2ttN3AwbiJ9.RtGYHmreKiyBfHuElgY
5     )
6 fig = px.scatter_mapbox(df, lat="latitude", lon="longitude", zoom=10)
7 return fig
8
9
10 def download_csv(df):
11 csv = df.to_csv(index=False)
12 b64 = base64.b64encode(csv.encode()).decode()
13 href = f'<a href="data:file/csv;base64,{b64}">Download CSV File</a> (right‐click and save l
14 return href
15
16
17 def display_results(gdf):
18 with st.spinner("Geocoding Hold tight..."):
19 time.sleep(5)
20 st.success("Done!")
21 st.write(gdf.head())
22 st.plotly_chart(display_map(gdf))
23 st.markdown(download_csv(gdf), unsafe_allow_html=True)

result.py hosted with ᤹


᤻ by GitHub
᤺ view raw

streamlit run app.py

7 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

8 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

9 of 10 11/11/2020, 4:20 PM
Build and Deploy Full Web applications Quickly with Python | by Abdis... https://medium.com/spatial-data-science/build-and-deploy-full-web-appli...

10 of 10 11/11/2020, 4:20 PM

You might also like