20
21
22def generate_table(dataframe, max_rows=10):
23 data = pd.read_excel('data/2018/aggregates-economic-activity/S7.1.xlsx', header = None)24 df = data[3:]
25 df.columns = df.iloc[0].fillna(value=pd.Series(range(100)))
26 return(dash_table.DataTable(
21
22
23def generate_table(dataframe, max_rows=10):
24 data = pd.read_excel('data/2018/economic-aggregates/S1.7r.xlsx', header = None) 25 df = data[6:]
26 df.columns = df.iloc[0].fillna(value=pd.Series(range(100)))
27 return(dash_table.DataTable(
62
63layout=app_layout()
64
65def filter(year, category, rows, labels, remove=False): 66 cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year]
67
68 filtered = rows[0:-1] if remove else rows
62
63layout=app_layout()
64
65def filter(year, category, rows, labels, remove=False): 66 cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year]
67
68 filtered = rows[0:-1] if remove else rows
24
25
26def generate_table(dataframe, max_rows=10):
27 data = pd.read_excel('data/2018/households/S5.2.xlsx', header = None)28 df = data[3:]
29 df.columns = df.iloc[0].fillna(value=pd.Series(range(100)))
30 return(dash_table.DataTable(
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()