From d878b7886c551c0b580576fc2aaa340c44edd88f Mon Sep 17 00:00:00 2001 From: "lucas.toniutti" <lucas.toniutti@etu.hesge.ch> Date: Mon, 12 Jun 2023 21:25:29 +0200 Subject: [PATCH] Exportation des simulations --- main/__pycache__/Decision.cpython-310.pyc | Bin 6583 -> 6583 bytes main/backend.py | 1 + main/templates/index.html | 56 +++++++++++++++------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/main/__pycache__/Decision.cpython-310.pyc b/main/__pycache__/Decision.cpython-310.pyc index 1d77cd921b83a35d7cc3672a2ca5f7f4f160f4ef..31410ac5b63ed1dafc47bab0ccf17b90fc033e4f 100644 GIT binary patch delta 20 bcmdmPyxo{PpO=@5fq{V`CBJ<m_Zmq6H^Bu? delta 20 bcmdmPyxo{PpO=@5fq{X+{zKzN?lqDCIi>}q diff --git a/main/backend.py b/main/backend.py index 724be19..cb890d2 100644 --- a/main/backend.py +++ b/main/backend.py @@ -430,6 +430,7 @@ def getResults(): dict['winrate'] = round(dec.export_result[0]['winrate']*100) dict['startBalance'] = dec.export_result[0]['StartBalance'] dict['endBalance'] = dec.export_result[0]['EndBalance'] + dict['market'] = dec.symbol dict['moneyProfits'] = {} dict['moneyLosses'] = {} for i in dict['startBalance']: diff --git a/main/templates/index.html b/main/templates/index.html index 7b7cb19..b93db00 100644 --- a/main/templates/index.html +++ b/main/templates/index.html @@ -319,7 +319,7 @@ </div> </div> <div class="col-sm mb-4 text-center"> - <button type="button" class="btn btn-success btn-lg"> + <button type="button" id="btnExportSimulation" class="btn btn-success btn-lg"> Enregistrer la simulation <i class="bi bi-save"></i> </button> </div> @@ -382,26 +382,38 @@ </div> </section> {% endif %} -<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> +<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <script src="https://d3js.org/d3.v7.min.js"></script> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> <script src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script> <script> const showMoreInfo = document.getElementById('btnShowMoreInfo'); + const exportSimulation = document.getElementById('btnExportSimulation'); const indicatorSelect = document.getElementById('indicatorSelect'); const SMA = document.getElementById('rowSMA'); const random = document.getElementById('rowRandom'); const test = document.getElementById('rowTest'); + var inProgress = {{ inProgress| lower }}; var chart = null; {% if fig != None %} chart = {{ fig | safe }}; - console.log(chart); Plotly.plot("chart", chart, {}); + showMoreInfo.addEventListener('click', function () { + if (moreResults.style.display === 'none') { + moreResults.style.display = 'block'; + showMoreInfo.innerHTML = 'Masquer les résultats'; + } else { + moreResults.style.display = 'none'; + showMoreInfo.innerHTML = 'Afficher plus de résultats'; + } + }); + exportSimulation.addEventListener('click', exportFile); + {% endif %} if (!inProgress && chart == null) { @@ -412,19 +424,6 @@ updateProgressBar(); } - - {% if fig != None %} - showMoreInfo.addEventListener('click', function () { - if (moreResults.style.display === 'none') { - moreResults.style.display = 'block'; - showMoreInfo.innerHTML = 'Masquer les résultats'; - } else { - moreResults.style.display = 'none'; - showMoreInfo.innerHTML = 'Afficher plus de résultats'; - } - }); - {% endif %} - function updateProgressBar() { $.ajax({ url: '/progress', @@ -455,5 +454,30 @@ random.style.display = 'none'; } } + + function exportFile() { + Plotly.toImage(document.getElementById('chart'), { format: 'png' }) + .then(function(dataUrl){ + const filename = "{{ res['market'] }}.json"; + const jsonData = { + image: dataUrl, + res: {{ res | tojson }}, + params: {{ stickyForm | tojson }} + } + const jsonStr = JSON.stringify(jsonData); + + let element = document.createElement('a'); + element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(jsonStr)); + element.setAttribute('download', filename); + + element.style.display = 'none'; + document.body.appendChild(element); + + element.click(); + + document.body.removeChild(element); + }) + } + </script> {% endblock %} \ No newline at end of file -- GitLab