File size: 11,077 Bytes
1cce1df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d38ada
1cce1df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="./favicon.ico" />
    <!-- Preload is necessary because we show these images when we disconnect from the server,
    but at that point we cannot load these images from the server -->
    <link rel="preload" href="./assets/gradient-yHQUC_QB.png" as="image" />
    <link rel="preload" href="./assets/noise-60BoTA8O.png" as="image" />
    <!-- Preload the fonts -->
    <link rel="preload" href="./assets/Lora-VariableFont_wght-B2ootaw-.ttf" as="font" crossorigin="anonymous" />
    <link rel="preload" href="./assets/PTSans-Regular-CxL0S8W7.ttf" as="font" crossorigin="anonymous" />
    <link rel="preload" href="./assets/PTSans-Bold-D9fedIX3.ttf" as="font" crossorigin="anonymous" />
    <link rel="preload" href="./assets/FiraMono-Regular-BTCkDNvf.ttf" as="font" crossorigin="anonymous" />
    <link rel="preload" href="./assets/FiraMono-Medium-DU3aDxX5.ttf" as="font" crossorigin="anonymous" />
    <link rel="preload" href="./assets/FiraMono-Bold-CLVRCuM9.ttf" as="font" crossorigin="anonymous" />

    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta name="description" content="a marimo app" />
    <link rel="apple-touch-icon" href="./apple-touch-icon.png" />
    <link rel="manifest" href="./manifest.json" />

    <script data-marimo="true">
      function __resizeIframe(obj) {
        var scrollbarHeight = 20; // Max between windows, mac, and linux

        function setHeight() {
          var element = obj.contentWindow.document.documentElement;
          // If there is no vertical scrollbar, we don't need to resize the iframe
          if (element.scrollHeight === element.clientHeight) {
            return;
          }

          // Create a new height that includes the scrollbar height if it's visible
          var hasHorizontalScrollbar = element.scrollWidth > element.clientWidth;
          var newHeight = element.scrollHeight + (hasHorizontalScrollbar ? scrollbarHeight : 0);

          // Only update the height if it's different from the current height
          if (obj.style.height !== `${newHeight}px`) {
            obj.style.height = `${newHeight}px`;
          }
        }

        // Resize the iframe to the height of the content and bottom scrollbar height
        setHeight();

        // Resize the iframe when the content changes
        const resizeObserver = new ResizeObserver((entries) => {
          setHeight();
        });
        resizeObserver.observe(obj.contentWindow.document.body);
      }
    </script>
    <marimo-filename hidden>notebook.py</marimo-filename>
    <marimo-mode data-mode='edit' hidden></marimo-mode>
    <marimo-version data-version='0.11.9' hidden></marimo-version>
    <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"theme": "light", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "cell_output": "above"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default", "overrides": {}}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "watcher_on_save": "lazy", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000}, "save": {"autosave": "off", "autosave_delay": 1000, "format_on_save": false}, "package_management": {"manager": "pip"}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{}' hidden></marimo-user-config>
    <marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config>
    <marimo-server-token data-token='123' hidden></marimo-server-token>
    <title>008 functions</title>
    <script type="module" crossorigin src="./assets/index-BiV-b1K2.js"></script>
    <link rel="stylesheet" crossorigin href="./assets/index-DkqMrX_B.css">
  <marimo-wasm hidden=""></marimo-wasm>
    <script>
        if (window.location.protocol === 'file:') {
            alert('Warning: This file must be served by an HTTP server to function correctly.');
        }
    </script>
    
    <style>
        #save-button {
            display: none !important;
        }
        #filename-input {
            display: none !important;
        }
    </style>
    <marimo-code hidden="" data-show-code="false">import%20marimo%0A%0A__generated_with%20%3D%20%220.11.9%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20%F0%9F%A7%A9%20Functions%0A%0A%20%20%20%20%20%20%20%20This%20tutorial%20is%20about%20an%20important%20topic%3A%20**functions.**%0A%0A%20%20%20%20%20%20%20%20A%20function%20is%20a%20reusable%20block%20of%20code%2C%20similar%20in%20spirit%20to%20a%20mathematical%20function.%20Each%20function%20has%20a%20**name**%2C%20and%20accepts%20some%20number%20of%20**arguments**.%20These%20arguments%20are%20used%20in%20the%20function%20%22body%22%20(its%20block%20of%20code)%2C%20and%20each%20function%20can%20**return**%20values.%0A%0A%20%20%20%20%20%20%20%20**Example.**%20Below%20is%20an%20example%20function.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20greet(your_name)%3A%0A%20%20%20%20%20%20%20%20return%20f%22Hello%2C%20%7Byour_name%7D!%22%0A%20%20%20%20return%20(greet%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22The%20keyword%20%60def%60%20starts%20the%20function%20definition.%20The%20function's%20**name**%20is%20%60greet%60.%20It%20accepts%20one%20**argument**%20called%20%60your_name%60.%20It%20then%20creates%20a%20string%20and%20**returns**%20it.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20In%20the%20next%20cell%2C%20we%20**call**%20the%20function%20with%20a%20value%20and%20assign%20its%20return%20value%20to%20a%20variable.%0A%0A%20%20%20%20%20%20%20%20**Try%20it!**%20Try%20changing%20the%20input%20to%20the%20function.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(greet)%3A%0A%20%20%20%20greeting%20%3D%20greet(your_name%3D%22%3Cyour%20name%20here%3E%22)%0A%20%20%20%20greeting%0A%20%20%20%20return%20(greeting%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20**Why%20use%20functions%3F**%20Functions%20help%20you%3A%0A%0A%20%20%20%20%20%20%20%20-%20Break%20down%20complex%20problems%0A%20%20%20%20%20%20%20%20-%20Create%20reusable%20code%20blocks%0A%20%20%20%20%20%20%20%20-%20Improve%20code%20readability%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Default%20parameters%0A%20%20%20%20%20%20%20%20Make%20your%20functions%20more%20flexible%20by%20providing%20default%20values.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20create_profile(name%2C%20age%3D18)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7Bname%7D%20is%20%7Bage%7D%20years%20old%22%0A%20%20%20%20return%20(create_profile%2C)%0A%0A%0A%40app.cell%0Adef%20_(create_profile)%3A%0A%20%20%20%20%23%20Example%20usage%0A%20%20%20%20example_name%20%3D%20%22Alex%22%0A%20%20%20%20example_profile%20%3D%20create_profile(example_name)%0A%20%20%20%20example_profile%0A%20%20%20%20return%20example_name%2C%20example_profile%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22You%20can%20also%20create%20functions%20that%20reference%20variables%20outside%20the%20function%20body.%20This%20is%20called%20'closing%20over'%20variables%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20base_multiplier%20%3D%202%0A%0A%20%20%20%20def%20multiplier(x)%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Create%20a%20function%20that%20multiplies%20input%20by%20a%20base%20value.%0A%0A%20%20%20%20%20%20%20%20This%20demonstrates%20how%20functions%20can%20'close%20over'%20%0A%20%20%20%20%20%20%20%20values%20from%20their%20surrounding%20scope.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20return%20x%20*%20base_multiplier%0A%20%20%20%20return%20base_multiplier%2C%20multiplier%0A%0A%0A%40app.cell%0Adef%20_(multiplier)%3A%0A%20%20%20%20print(%5Bmultiplier(num)%20for%20num%20in%20%5B1%2C%202%2C%203%5D%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Returning%20multiple%20values%0A%0A%20%20%20%20%20%20%20%20Functions%20can%20return%20multiple%20values%3A%20just%20separate%20the%20values%20to%20return%20by%0A%20%20%20%20%20%20%20%20commas.%20Check%20out%20the%20next%20cell%20for%20an%20example.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20def%20weather_analysis(temp)%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Analyze%20weather%20based%20on%20temperature.%0A%0A%20%20%20%20%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20(float)%3A%20Temperature%20in%20Celsius%0A%0A%20%20%20%20%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tuple%3A%20Weather%20status%2C%20recommendation%2C%20warning%20level%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20if%20temp%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Freezing%22%2C%20%22Wear%20heavy%20coat%22%2C%20%22High%22%0A%20%20%20%20%20%20%20%20elif%200%20%3C%20temp%20%3C%3D%2015%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Cold%22%2C%20%22Layer%20up%22%2C%20%22Medium%22%0A%20%20%20%20%20%20%20%20elif%2015%20%3C%20temp%20%3C%3D%2025%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Mild%22%2C%20%22Comfortable%20clothing%22%2C%20%22Low%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Hot%22%2C%20%22Stay%20hydrated%22%2C%20%22High%22%0A%20%20%20%20return%20(weather_analysis%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20temperature%20%3D%2025%0A%20%20%20%20return%20(temperature%2C)%0A%0A%0A%40app.cell%0Adef%20_(temperature%2C%20weather_analysis)%3A%0A%20%20%20%20status%2C%20recommendation%2C%20warning_level%20%3D%20weather_analysis(temperature)%0A%20%20%20%20status%2C%20recommendation%2C%20warning_level%0A%20%20%20%20return%20recommendation%2C%20status%2C%20warning_level%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A</marimo-code></head>
  <body>
    <div id="root"></div>
  </body>
</html>