Vadim-Voronin commited on
Commit
f2dde7a
1 Parent(s): cd90442

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import gspread
5
+ import folium
6
+ import requests as r
7
+ from datetime import datetime
8
+ from datetime import date
9
+ from bs4 import BeautifulSoup as bs
10
+ from shapely.geometry import Point
11
+ from google.oauth2.service_account import Credentials
12
+ from streamlit_folium import folium_static
13
+ import pickle
14
+ import os
15
+ #API='80ccf867-5353-4474-a01c-d1299496eca7'
16
+ API='deb65d93-a965-4d1f-a09e-5a047ea959d7'
17
+
18
+
19
+
20
+ @st.cache
21
+ def make_date(a):
22
+ try:
23
+ return datetime.strptime(a, "%d.%m.%Y").date()
24
+ except:
25
+ return date(2000, 3, 14)
26
+
27
+
28
+
29
+ @st.cache
30
+ def GET_COORDS_YANDEX(adress):
31
+ ADRESS=adress.replace(' ', '+')
32
+ result=r.get(f'https://geocode-maps.yandex.ru/1.x/?apikey={API}&geocode={ADRESS}&results=1')
33
+ res_b_xml=bs(result.content, "lxml")
34
+ try:
35
+ coords=res_b_xml.find('pos').text.split()
36
+ res=[float(a) for a in coords]
37
+ return Point(res)
38
+ except:
39
+ return None
40
+
41
+
42
+
43
+ def first_try(data):
44
+ old_data=[data[0]]
45
+ len_old_data=1
46
+ return old_data, len_old_data
47
+
48
+
49
+
50
+ def import_data():
51
+ scopes = [
52
+ 'https://www.googleapis.com/auth/spreadsheets',
53
+ 'https://www.googleapis.com/auth/drive'
54
+ ]
55
+
56
+ credentials = Credentials.from_service_account_file(
57
+ 'test-project-for-telegram-bot-bcb5e887f016.json',
58
+ scopes=scopes
59
+ )
60
+
61
+ gc = gspread.authorize(credentials)
62
+ spr = gc.open_by_url("https://docs.google.com/spreadsheets/d/1Ol9039HJ99Q8xVVp1Wwz5iBdURxEKHLIcEvyqUc2pZM/edit#gid=642560994")
63
+ wks=spr.worksheet('Консолидированный список')
64
+ new_data=wks.get_all_values()
65
+ new_data[0].pop(-1)
66
+ new_data[0].pop(-1)
67
+ with open('old_data.pickle', 'rb') as f:
68
+ if os.stat('old_data.pickle').st_size == 0:
69
+ old_data, len_old_data=first_try(new_data)
70
+ else:
71
+ old_data=pickle.load(f)
72
+ len_old_data=len(old_data)
73
+ list_to_geocode=new_data[len_old_data::]
74
+ for lead in list_to_geocode:
75
+ lead.pop(-1)
76
+ lead.pop(-1)
77
+ lead.append(GET_COORDS_YANDEX(lead[8]))
78
+ #lead.append('thee must be coords')
79
+ lead.append(make_date(lead[3]))
80
+ for lead in list_to_geocode:
81
+ old_data.append(lead)
82
+ if len(old_data[0])!=len(old_data[1]):
83
+ old_data[0].append('coords1')
84
+ old_data[0].append('date1')
85
+ with open('old_data.pickle', 'wb') as f:
86
+ pickle.dump(old_data, f)
87
+ DD=pd.DataFrame(old_data[1::], columns=old_data[0])
88
+ return DD
89
+
90
+
91
+ OBJECTS=import_data()
92
+ OBJECTS=pd.DataFrame(
93
+ {
94
+ 'комнатность':OBJECTS['Кол-во комнат'].values,
95
+ 'этаж':OBJECTS['Этаж'].values,
96
+ 'площадь':OBJECTS['Площадь '].values,
97
+ 'ремонт':OBJECTS['Ремонт'].values,
98
+ 'Диапазон предварительной оценки':OBJECTS['Предварительная оценка (рынок / выкуп) '].values,
99
+ 'Дата оценки':OBJECTS['date1'].values,
100
+ 'geometry':OBJECTS['coords1'].values
101
+ }
102
+ )
103
+ st.write(OBJECTS.head(3))
104
+ GEO_OBJECTS=gpd.GeoDataFrame(OBJECTS)
105
+
106
+
107
+ with st.sidebar:
108
+ st.header('Введите параметры оценок')
109
+ min_date = st.date_input("введите минимальную дату оценки", date(2019, 7, 6))
110
+ max_date = st.date_input("введите максимальную дату оценки", date(2019, 7, 6))
111
+ rooms = st.multiselect('комантность', ['ст', '1', '2', '3', '4'], ['1', '2'])
112
+ areamin, areamax = st.slider('Select a range of values', 0.0 , 200.0, (25.0, 75.0))
113
+ start_adress = st.text_input("адресс текущей оценки")
114
+
115
+
116
+ #Data_to_plot=GEO_OBJECTS.loc[(GEO_OBJECTS['Дата оценки']>min_date)&(GEO_OBJECTS['Дата оценки']<min_date)&(GEO_OBJECTS['комнатность'] in rooms)&(GEO_OBJECTS['площадь']>areamin)&(GEO_OBJECTS['площадь']<areamax)]
117
+
118
+
119
+ st.subheader('Карта оценок')
120
+ m = folium.Map(location=[55.87890, 37.71943], zoom_start=10, tiles='CartoDB positron')
121
+
122
+ #for A in Data_to_plot.values:
123
+ # tag=''
124
+ # lat=A[5].y
125
+ # log=A[5].x
126
+ # for feature in A[:-2]:
127
+ # tag+=feature.replace('\"', '').replace("'","").replace("/"," ")+' '
128
+ #if len(tag)>40:
129
+ # tag=tag[5:]
130
+ #tag=f'хуувая квартира №{i}'
131
+ # tag+=str(A[-1])
132
+ # folium.Marker(location=[lat, log],
133
+ # popup = tag).add_to(m)
134
+
135
+ folium_static(m)
136
+