Advertisement
askanton

Відтворюємо еволюцію на #python | Приклад візуалізації процесів

Jun 17th, 2023
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. import time
  2. from flask import Flask, render_template_string, session
  3. import random
  4.  
  5. app = Flask(__name__)
  6. app.secret_key = "your_secret_key"
  7. heritage = 0
  8. stop = 0
  9.  
  10.  
  11. @app.route("/")
  12. def index():
  13.     global heritage, stop
  14.     template = """
  15.    <!DOCTYPE html>
  16.    <html>
  17.    <head>
  18.        <title>Mountain Visualization</title>
  19.        <style>
  20.            body {
  21.                margin: 0;
  22.            }
  23.            .container {
  24.                position: relative;
  25.                display: flex;
  26.            }
  27.            .mountain {
  28.                position: relative;
  29.                width: 0;
  30.                height: 0;
  31.                border-left: 75px solid transparent;
  32.                border-right: 75px solid transparent;
  33.                border-bottom: 200px solid gray;
  34.                margin-right: -10px;
  35.                transform: translateY(50px);
  36.            }
  37.            .plateau {
  38.                background-color: green;
  39.                width: 350px;
  40.                height: 50px;
  41.                margin-right: -10px;
  42.                transform: translateY(200px);
  43.            }
  44.            .plateau_wide {
  45.                background-color: green;
  46.                width: 950px;
  47.                height: 50px;
  48.                margin-right: -10px;
  49.                transform: translateY(200px);
  50.            }
  51.            .cliff {
  52.                background-color: black;
  53.                width: 350px;
  54.                height: 150px;
  55.                margin-right: -10px;
  56.                transform: translateY(250px);
  57.            }
  58.            .circle {
  59.                position: absolute;
  60.                border-radius: 50%;
  61.            }
  62.            .circle.fat {
  63.                background-color: blue;
  64.                width: 25px;
  65.                height: 25px;
  66.            }
  67.            .circle.medium {
  68.                background-color: orange;
  69.                width: 15px;
  70.                height: 15px;
  71.            }
  72.            .circle.slim {
  73.                background-color: red;
  74.                width: 5px;
  75.                height: 5px;
  76.            }
  77.            .circle.new {
  78.                background-color: yellow;
  79.                width: 40px;
  80.                height: 40px;
  81.            }
  82.  
  83.            .color-count {
  84.                transform: translateY(-60px);
  85.                font-weight: bold;
  86.            }
  87.        </style>
  88.        <meta http-equiv="refresh" content="3">
  89.    </head>
  90.    <body>
  91.        <div class="container">
  92.            <div class="cliff"></div>
  93.            <div class="plateau"></div>
  94.            <div class="mountain"></div>
  95.            <div class="plateau_wide"></div>
  96.            {% for circle in circles %}
  97.                <div class="circle {{ circle.circle }}" style="bottom: {{ circle.bottom }}; left: {{ circle.left }}px;"></div>
  98.            {% endfor %}
  99.        </div>
  100.        <div class="color-count">
  101.            Blue Circles: {{ color_counts.blue }}
  102.        </div>
  103.        <div class="color-count">
  104.            Orange Circles: {{ color_counts.orange }}
  105.        </div>
  106.        <div class="color-count">
  107.            Red Circles: {{ color_counts.red }}
  108.        </div>
  109.        <div class="color-count">
  110.            New Circles: {{ color_counts.new }}
  111.        </div>
  112.        <div class="color-count">
  113.            Generation: {{ color_counts.gen }}
  114.        </div>
  115.    </body>
  116.    </html>
  117.    """
  118.  
  119.     if "refresh_count" not in session:
  120.         session["refresh_count"] = 0
  121.  
  122.     i = session["refresh_count"]
  123.     if stop == 1:
  124.         time.sleep(1000)
  125.  
  126.     def create_circle(fat):
  127.         if fat >= 40 and fat <= 100:
  128.             circle = "fat"
  129.             left = random.randint(350, 650)
  130.             bottom = "0px"
  131.         elif fat >= 20 and fat < 40:
  132.             circle = "medium"
  133.             left = random.randint(50, 300)
  134.             bottom = "-55px"
  135.         elif fat > 0 and fat < 20:
  136.             circle = "slim"
  137.             left = random.randint(720, 780)
  138.             bottom = "50px"
  139.         if fat == 0:
  140.             circle = "new"
  141.             left = random.randint(800, 900)
  142.             bottom = "0px"
  143.  
  144.         return {"circle": circle, "bottom": bottom, "left": left}
  145.  
  146.     if heritage == 0:
  147.         circles = [create_circle(random.randint(19, 100)) for _ in range(20)]
  148.     elif heritage == 1:
  149.         circles = [create_circle(random.randint(1, 100)) for _ in range(20)]
  150.     elif heritage == 2:
  151.         circles = [create_circle(random.randint(1, 100)) for _ in range(20)]
  152.         circles.append({"circle": "new", "bottom": "0px", "left": 900})
  153.         stop = 1
  154.     color_counts = {
  155.         "blue": sum(circle["circle"] == "fat" for circle in circles),
  156.         "orange": sum(circle["circle"] == "medium" for circle in circles),
  157.         "red": sum(circle["circle"] == "slim" for circle in circles),
  158.         "new": sum(circle["circle"] == "new" for circle in circles),
  159.         "gen": i,
  160.     }
  161.     if color_counts["red"] > 6:
  162.         heritage = 2
  163.     elif color_counts["red"] > 0:
  164.         heritage = 1
  165.     else:
  166.         heritage = 0
  167.     session["refresh_count"] += 1
  168.     return render_template_string(template, circles=circles, color_counts=color_counts)
  169.  
  170.  
  171. if __name__ == "__main__":
  172.     app.run()
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement