"""
### Summary
The app allows you to upload a submission and analyze how well you would have done in previous years’ competitions. 
* The Public leaderboard is usually full of leaky submissions making it hard to determine the quality of a submission. The Public leaderboard is included here for comparison. It is updated everytime the app is run.
* The Average leaderboard shows the average score of the nth place teams. For example, if your submission places 10th on the Average leaderboard then your score is slightly better the average of the 10th place teams in the previous competitions and slightly worse than the average of the 9th place teams in the previous competitions.
* The 2018 - 2019 leaderboards are exact copys from previous competitions.You can use them to view where your submission would have placed in those competitions.


### Run on the App on Kaggle
Fork and edit this notebook. Run all cells of the notebook and view the app in a separate tab using the url generated by [ngrok](https://ngrok.com/). 

**Important: The app needs a backend to run. You must fork and edit the notebook. You won't be able to view the app from a static Kaggle notebook.**

### Local Installation
If you prefer to run locally (it should be faster) follow the following instructions: 
1. **Run the Wave Server:** Follow the instructions [here](https://h2oai.github.io/wave/docs/installation) to download and run the latest Wave Server, a requirement for apps. Note: If you have a version of Wave older than or equal to 0.12.0, you will need to reinstall Wave with a newer version. 

2. **Download the App:** Download the [app code](https://www.kaggle.com/mmotoki/womens-leaderboard-analyzer-app-2021) from kaggle. Open a terminal in the `womens_leaderboard` directory and create a `tmp` folder for uploded files.
```bash
$ mkdir tmp
```

3. **Setup Your Python Environment**:
```bash
$ make setup
$ source venv/bin/activate
```
4. **Run the App:**
```bash
$ wave run leaderboard.app
```
5. **View the App:** Point your favorite web browser to [localhost:10101](http://localhost:10101)


### Acknowledgements 
* Thank you to [nagiss](https://www.kaggle.com/nagiss). This app was heavily influenced by nagiss's notebook: [Santa2020 Stable Rating Estimation & LeaderBoards](https://www.kaggle.com/nagiss/santa2020-stable-rating-estimation-leaderboards). Much of the code for formatting tables comes from that notebook. 
* Thank you to [KS](https://www.kaggle.com/ks2019) who also influenced this app with his [discussion](https://www.kaggle.com/c/ncaam-march-mania-2021/discussion/222390) on making sense of the public leaderboard. 
* Thank you to [beluga](https://www.kaggle.com/gaborfodor) who provided feedback and reviewed the app--you can thank him for the competition points idea.

"""
# Downloading and installing wave. We'll run it in following cell.
!wget https://github.com/h2oai/wave/releases/download/v0.12.1/wave-0.12.1-linux-amd64.tar.gz
!tar -zxvf wave-0.12.1-linux-amd64.tar.gz
!chmod +x ./wave-0.12.1-linux-amd64/waved
!pip install pyngrok -q #ngrok will be needed for adress tunneling.
from pyngrok import ngrok
from subprocess import Popen, run
import sys
import os
import time
from IPython.core.display import display, HTML

os.chdir('wave-0.12.1-linux-amd64')
waved_process = Popen("./waved", shell=True) # We run ./waved at backend so that our app will be able to run on the browser.
os.chdir('..')
# Setting up the working directory so that the app will be run based on them.
!mkdir -p tmp
!mkdir -p data
!mkdir -p leaderboard
!cp -R ../input/womens-leaderboard-analyzer-app-2021/womens_leaderboard/data/. data
!cp -R ../input/womens-leaderboard-analyzer-app-2021/womens_leaderboard/leaderboard/. leaderboard
!pip install h2o-wave==0.12.1
try:
    app_process.kill()
except:
    pass
app_process = Popen(["wave", "run", "leaderboard.app"])
url = ngrok.connect(addr=10101).public_url
print(f"Successfully established ngrok tunnel, visit the app at {url}")
print("The app needs a backend to run. If you aren't already doing so, you must fork and edit the notebook. You won't be able to view the app from a static Kaggle notebook.")