用PYTHON和STREMLIT从GITHUB加载JSON数据

2021-12-01 12:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,提前感谢你们的帮助

我一直在使用Streamlight和Python进行开发,现在我想在Heroku中部署所有的想法

这是我第一次在heroku中部署应用程序。我想从github加载json文件,因为我想每周用statsbomb数据更新应用程序

到目前为止,我一直在从我的计算机加载json文件,请查看随附的代码:

import streamlit as st
import pandas as pd
import json
import os
from pandas import json_normalize
from flatten_json import flatten

import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import numpy as np
from PIL import Image
import urllib, json

### NAME OF THE APP ###
x = st.sidebar.header(' Analytics')  # 👈 this is a widget

### SEASON SELECTOR ###
file_to_charge2 = st.sidebar.selectbox('temporada',('Temporada 2018-2019','Temporada 2017-2018','Temporada 2016-2017'))

if file_to_charge2 == 'Temporada 2018-2019':
    file_to_charge = '4.json'
elif file_to_charge2 == 'Temporada 2017-2018':
    file_to_charge2 = '1.json'
elif file_to_charge2== 'Temporada 2016-2017':
    file_to_charge = '2.json'


### MATCH SELECTOR ###

home_team = st.sidebar.selectbox('Equipo juega en casa:',('Athletic Bilbao', 'Atlético Madrid', 'Barcelona', 'Celta Vigo', 'Deportivo Alavés', 'Deportivo La Coruna', 'Eibar', 'Espanyol', 'Getafe', 'Girona', 'Granada', 'Las Palmas', 'Leganés', 'Levante', 'Málaga', 'Osasuna', 'Rayo Vallecano', 'Real Betis', 'Real Madrid', 'Real Sociedad', 'Real Valladolid', 'Sevilla', 'Sporting Gijón', 'Valencia', 'Villarreal'))
away_team = st.sidebar.selectbox('Equipo fuera de casa:',('Athletic Bilbao', 'Atlético Madrid', 'Barcelona', 'Celta Vigo', 'Deportivo Alavés', 'Deportivo La Coruna', 'Eibar', 'Espanyol', 'Getafe', 'Girona', 'Granada', 'Las Palmas', 'Leganés', 'Levante', 'Málaga', 'Osasuna', 'Rayo Vallecano', 'Real Betis', 'Real Madrid', 'Real Sociedad', 'Real Valladolid', 'Sevilla', 'Sporting Gijón', 'Valencia', 'Villarreal'))
#json_normalize(my_data3, sep='_').assign(match_id=file_name[:-5])



###LOAD THE JSON FILE FROM COMPUTER### 

st.title('> Datos por temporada:')
dfpartidos = json.load(open(os.path.expanduser('~/Desktop/DATOS/open-data-master/data/matches/11/' + file_to_charge), 'r', encoding='utf-8'))



### DATAFRAME OF THE SEASON ###
FIELDS = ['match_id','match_week',"home_team.home_team_name",'away_team.away_team_name','home_score','away_score','referee.name']
dfdef = pd.json_normalize(dfpartidos)
p = dfdef[FIELDS]

### DATAFRAME OF THE TEAMS ###
l = p.loc[p['home_team.home_team_name'] == home_team, :]
l = l.loc[l['away_team.away_team_name'] == away_team, :]




#TABLE WITH DATA OF THE SELECTED SEASON:
st.subheader('Datos de la  {} :'.format(file_to_charge2))
st.write(p)

#TABLE WITH DATA OF THE SELECETED TEAMS

st.write('Home Team:',home_team,'VS','Away Team:',away_team)

st.write(l)

这段代码运行良好,可以分析惊人的数据。但是如果我想更新应用程序。这就是我需要从github启动json文件的原因。我做了一些失败的测试,但我有一些问题。请查看附件中的代码和错误

import streamlit as st
import pandas as pd
import json
import os
from pandas import json_normalize
from flatten_json import flatten

import matplotlib.pyplot as plt
from matplotlib.patches import Arc
import numpy as np
from PIL import Image
import urllib, json
import plotly.express as px
import altair as atl
import re, json, requests

x = st.sidebar.header(' Analytics')  # 👈 this is a widget

####
file_to_charge2 = st.sidebar.selectbox('temporada',
                                       ('Temporada 2018-2019', 'Temporada 2017-2018', 'Temporada 2016-2017'))

if file_to_charge2 == 'Temporada 2018-2019':
    file_to_charge = '4.json'
elif file_to_charge2 == 'Temporada 2017-2018':
    file_to_charge2 = '1.json'
elif file_to_charge2 == 'Temporada 2016-2017':
    file_to_charge = '2.json'


### busqueda de partidos ###

home_team = st.sidebar.selectbox('Equipo juega en casa:', (
'Athletic Bilbao', 'Atlético Madrid', 'Barcelona', 'Celta Vigo', 'Deportivo Alavés', 'Deportivo La Coruna', 'Eibar',
'Espanyol', 'Getafe', 'Girona', 'Granada', 'Las Palmas', 'Leganés', 'Levante', 'Málaga', 'Osasuna', 'Rayo Vallecano',
'Real Betis', 'Real Madrid', 'Real Sociedad', 'Real Valladolid', 'Sevilla', 'Sporting Gijón', 'Valencia', 'Villarreal'))
away_team = st.sidebar.selectbox('Equipo fuera de casa:', (
'Athletic Bilbao', 'Atlético Madrid', 'Barcelona', 'Celta Vigo', 'Deportivo Alavés', 'Deportivo La Coruna', 'Eibar',
'Espanyol', 'Getafe', 'Girona', 'Granada', 'Las Palmas', 'Leganés', 'Levante', 'Málaga', 'Osasuna', 'Rayo Vallecano',
'Real Betis', 'Real Madrid', 'Real Sociedad', 'Real Valladolid', 'Sevilla', 'Sporting Gijón', 'Valencia', 'Villarreal'))
# json_normalize(my_data3, sep='_').assign(match_id=file_name[:-5])




### TRYING TO LOAD DATA FROM GITHUB // THE PROBLEM ###
url = 'https://raw.github.com/statsbomb/open-data/matches/11/' + file_to_charge
resp = requests.get(url)
st.title('> Datos por temporada:')
dfpartidos = json.loads(resp.text)



### DATAFRAME OF THE SEASON ###
FIELDS = ['match_id', 'match_week', "home_team.home_team_name", 'away_team.away_team_name', 'home_score', 'away_score',
          'referee.name']
dfdef = pd.json_normalize(dfpartidos)
p = dfdef[FIELDS]

# DATAFRAME SEGÚN EQUIPOS FILTRADOS
l = p.loc[p['home_team.home_team_name'] == home_team, :]
l = l.loc[l['away_team.away_team_name'] == away_team, :]

# TABLE WITH DATA OF THE SELECTED SEASON:
st.subheader('Datos de la  {} :'.format(file_to_charge2))
st.write(p)

# TABLE WITH DATA OF THE SELECETED TEAMS

st.write('Home Team:', home_team, 'VS', 'Away Team:', away_team)

st.write(l)

从github加载json日期时出错。

JSONDecodeError: Extra data: line 1 column 4 (char 3)
Traceback:
File "/Users/DOMIN2662/Desktop/streamlit/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 324, in _run_script
    exec(code, module.__dict__)
File "/Users/DOMIN2662/Desktop/streamlit/duda22.py", line 50, in <module>
    dfpartidos = json.loads(resp.text)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)

我想强调comunity的支持,我希望在未来我能帮助其他开发者