file_download-dynamic-temp-file.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. import os
  17. from decimal import Decimal, getcontext
  18. from tempfile import NamedTemporaryFile
  19. from taipy.gui import Gui, download
  20. def pi(precision: int) -> list[int]:
  21. """Compute Pi to the required precision.
  22. Adapted from https://docs.python.org/3/library/decimal.html
  23. """
  24. saved_precision = getcontext().prec # Save precision
  25. getcontext().prec = precision
  26. three = Decimal(3) # substitute "three=3.0" for regular floats
  27. lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
  28. while s != lasts:
  29. lasts = s
  30. n, na = n + na, na + 8
  31. d, da = d + da, da + 32
  32. t = (t * n) / d
  33. s += t # type: ignore[assignment]
  34. digits = []
  35. while s != 0:
  36. integral = int(s)
  37. digits.append(integral)
  38. s = (s - integral) * 10
  39. getcontext().prec = saved_precision
  40. return digits
  41. # Remove the temporary file
  42. def clean_up(state):
  43. os.remove(state.temp_path)
  44. # Generate the digits, save them in a CSV temporary file, then trigger a download action
  45. # for that file.
  46. def download_pi(state):
  47. digits = pi(state.precision)
  48. with NamedTemporaryFile("r+t", suffix=".csv", delete=False) as temp_file:
  49. state.temp_path = temp_file.name
  50. temp_file.write("index,digit\n")
  51. for i, d in enumerate(digits):
  52. temp_file.write(f"{i},{d}\n")
  53. download(state, content=temp_file.name, name="pi.csv", on_action=clean_up)
  54. # Initial precision
  55. precision = 10
  56. # Stores the path to the temporary file
  57. temp_path = None
  58. page = """
  59. Precision:
  60. <|{precision}|slider|min=2|max=10000|>
  61. <|{None}|file_download|on_action=download_pi|label=Download Pi digits|>
  62. """
  63. if __name__ == "__main__":
  64. Gui(page).run(title="File Download - Dynamic content")