123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- # Copyright 2021-2024 Avaiga Private Limited
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- # the License. You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- # specific language governing permissions and limitations under the License.
- # -----------------------------------------------------------------------------------------
- # To execute this script, make sure that the taipy-gui package is installed in your
- # Python environment and run:
- # python <script>
- # -----------------------------------------------------------------------------------------
- import os
- from decimal import Decimal, getcontext
- from tempfile import NamedTemporaryFile
- from taipy.gui import Gui, download
- def pi(precision: int) -> list[int]:
- """Compute Pi to the required precision.
- Adapted from https://docs.python.org/3/library/decimal.html
- """
- saved_precision = getcontext().prec # Save precision
- getcontext().prec = precision
- three = Decimal(3) # substitute "three=3.0" for regular floats
- lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
- while s != lasts:
- lasts = s
- n, na = n + na, na + 8
- d, da = d + da, da + 32
- t = (t * n) / d
- s += t # type: ignore[assignment]
- digits = []
- while s != 0:
- integral = int(s)
- digits.append(integral)
- s = (s - integral) * 10
- getcontext().prec = saved_precision
- return digits
- # Remove the temporary file
- def clean_up(state):
- os.remove(state.temp_path)
- # Generate the digits, save them in a CSV temporary file, then trigger a download action
- # for that file.
- def download_pi(state):
- digits = pi(state.precision)
- with NamedTemporaryFile("r+t", suffix=".csv", delete=False) as temp_file:
- state.temp_path = temp_file.name
- temp_file.write("index,digit\n")
- for i, d in enumerate(digits):
- temp_file.write(f"{i},{d}\n")
- download(state, content=temp_file.name, name="pi.csv", on_action=clean_up)
- # Initial precision
- precision = 10
- # Stores the path to the temporary file
- temp_path = None
- page = """
- Precision:
- <|{precision}|slider|min=2|max=10000|>
- <|{None}|file_download|on_action=download_pi|label=Download Pi digits|>
- """
- if __name__ == "__main__":
- Gui(page).run(title="File Download - Temporary file")
|