12 Streamlit, Recursion, Misc

image.png լուսանկարի հղումը, Հեղինակ՝ Vazgen

Open In Colab (ToDo)

Song reference - ToDo

🎦 Տեսադասեր + լրացուցիչ (ToDo)

ToDo 1. Տեսություն 2025
2. Տեսություն 2023 (ToDo)
3. Գործնական 2025
4. Գործնական 2023 (ToDo)
5. Որոշ տնայինների լուծումներ (ToDo)

Google Forms ToDo

📚 Նյութը

Streamlit

  • Gallery (official) - https://streamlit.io/gallery
  • Gallery (repo) - https://github.com/jrieke/best-of-streamlit
  • Example - https://prettymapp.streamlit.app/?ref=streamlit-io-gallery-favorites
  • Cheatsheet - https://luluwu-cheatsheet.streamlit.app/
  • Reference - https://docs.streamlit.io/develop/api-reference
!pip install streamlit
!conda install streamlit

place the code below into file app.py

"""
streamlit run streamlit_app.py

- Gallery (official) - https://streamlit.io/gallery
- Gallery (repo) - https://github.com/jrieke/best-of-streamlit
- Example - https://prettymapp.streamlit.app/?ref=streamlit-io-gallery-favorites
- Cheatsheet - https://luluwu-cheatsheet.streamlit.app/
- Reference - https://docs.streamlit.io/develop/api-reference

pip install streamlit
conda install streamlit
"""
from time import sleep

import pandas as pd
import streamlit as st
import plotly.express as px

# with st.echo():
#     # congrats
#     st.balloons()
#     st.snow()
    


with st.echo():
    # ---------- Displaying Text  ----------
    # Title and Header
    st.title("My Streamlit App")
    st.header("Welcome!")

with st.echo():
    # Text
    st.subheader("Text")
    st.text("This is a text block.")

with st.echo():
    # Markdown
    st.subheader("Markdown")
    st.markdown("This is a markdown block. You can write *italic*, **bold**, or even use [links](https://www.streamlit.io/).")

st.divider()
# Latex
st.subheader("Latex")
with st.echo():
    st.latex(r"Y = \alpha + \beta X_i")

with st.echo():
    st.write("You can also use LaTeX for mathematical expressions like this: $E = mc^2$")


st.divider()
st.subheader("Table")
with st.echo():
    # ---------- Displaying DataFrames  ----------
    # Displaying a Table
    st.dataframe([{ 'Name': "Գինի", 'Age': 3 }, { 'Name': "Պանիր", 'Age': 1 }])

with st.echo():
    # DataFrame
    data = {'Name': ['Մալխաս', 'Բարխուդարում', 'Մատատյան ու Մարոխյան'], 'Age': [3, 4, 1]}
    df = pd.DataFrame(data)
    st.dataframe(df)

with st.echo():
    st.json({
        "Product": "Պանիր",
        "Type": "Չանախ",
    })

# Plotly
st.subheader("Plotly Bar Chart")

with st.echo():
    st.divider()
    fig = px.bar(df, x='Name', y='Age')
    st.plotly_chart(fig)

with st.echo():
    st.line_chart([10, 14, 19, 20, 25, 30, 35, 40, 45, 50, 55, 60])

st.divider()
st.subheader("Interactive Widgets")
with st.echo():
    # Interactive Widgets
    # # # Button
    if st.button("Click me!"):
        st.write("Button clicked!")

with st.echo():
    # Checkbox
    checkbox_value = st.checkbox("Check me!")
    if checkbox_value:
        st.write("Checkbox checked!")

with st.echo():
    # Selectbox
    option = st.selectbox("Choose an option", ['Option 1', 'Option 2', 'Option 3'])
    st.write("You selected:", option)

with st.echo():
    # Slider
    slider_value = st.slider("Slide me", 0, 100, step=5)
    st.write("Slider value:", slider_value)

with st.echo():
    num_input_value = st.number_input("Num input", min_value=0.0, max_value=100., step=5.0) # պետքա float լինի
    st.write("Number input value:", num_input_value)

with st.echo():
    text = st.text_input("please input text")
    st.write("Text input value:", text)

with st.echo():
    # File Uploader
    uploaded_file = st.file_uploader("Upload a file")
    if uploaded_file is not None:
        st.write("File uploaded!")

st.divider()
# Displaying Progress
st.subheader("Progress")

# with st.echo():
#     progress_bar = st.progress(0)

#     for i in range(100):
#         sleep(0.1)
#         progress_bar.progress(i + 1)

with st.echo():
    # Sidebar
    st.sidebar.header("Sidebar")
    st.sidebar.text("This is a sidebar.")

with st.echo():
    rating1 = st.feedback("stars")
    rating2 = st.feedback("faces")
    st.write(f"Rating 1: {rating1}")
    st.write(f"Rating 2: {rating2}")

with st.echo():
    color = st.color_picker("Pick a color")
    st.write("Selected color:", color)
    
with st.echo():
    enable = st.checkbox("Enable camera")
    picture = st.camera_input("Take a picture", disabled=not enable)

    if picture:
        st.image(picture)
        
with st.echo():
    audio_value = st.audio_input("Record a voice message")

    if audio_value:
        st.audio(audio_value)
        
with st.echo():
    col1, col2, col3 = st.columns(3)

    with col1:
        st.header("A cat")
        st.image("https://static.streamlit.io/examples/cat.jpg")

    with col2:
        st.header("A dog")
        st.image("https://static.streamlit.io/examples/dog.jpg")

    with col3:
        st.header("An owl")
        st.image("https://static.streamlit.io/examples/owl.jpg")

Recursion

einstein.gif

einstein.gif

https://xn–y9aa8b7a3an.xn–y9a3aq/%D5%8C%D5%A5%D5%AF%D5%B8%D6%82%D6%80%D5%BD%D5%AB%D5%A1

Նյութեր

https://realpython.com/python-recursion/

Ֆանտաստիկ վիդեո - https://www.youtube.com/watch?v=ngCos392W4w
Շատ հայտնի խնդիր (Հանոյի աշտարակ) ռեկուրսիայով լուծվող - https://www.youtube.com/watch?v=rf6uf3jNjbo

See https://github.com/HaykTarkhanyan/python_math_ml_course/blob/main/python/Recursion.ipynb

Memes

Google search

image.png

image.png

image.png

image.png

image.png

image.png
def function():
    x = 509
    function()

function()
---------------------------------------------------------------------------

RecursionError                            Traceback (most recent call last)

Input In [9], in <cell line: 5>()

      2     x = 509

      3     function()

----> 5 function()



Input In [9], in function()

      1 def function():

      2     x = 509

----> 3     function()



Input In [9], in function()

      1 def function():

      2     x = 509

----> 3     function()



    [... skipping similar frames: function at line 3 (2970 times)]



Input In [9], in function()

      1 def function():

      2     x = 509

----> 3     function()



RecursionError: maximum recursion depth exceeded

! Ֆակտորիալ

6! = 6 * 5! = 6 * (5 * 4!) = 
def factiorial(n):
    return ...


4! = 4 * 3! = 4 * 3 * 2! = 4 * 3 *

image.png

image.png
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n - 1)
    
factorial(4)
24
def factorial(n):
    print(f"factorial() called with n = {n}")
    if n == 1:
        return_value = 1
    else:
        return_value =  n * factorial(n - 1)
    print(f"-> factorial({n}) returns {return_value}")
    return return_value


factorial(4)

f(4) = 4 * 6 | 3 * 2 | 2 * 1

f(4) = 4 * f(3) = 4 * 3 * f(2) = 4 * 3 * 2 * f(1) = 4 * 3 * 2 * 1 = 24 
factorial() called with n = 4
factorial() called with n = 3
factorial() called with n = 2
factorial() called with n = 1
-> factorial(1) returns 1
-> factorial(2) returns 2
-> factorial(3) returns 6
-> factorial(4) returns 24
24

1-n թվերի գումարը

image.png

image.png
def sum_natural(n):
    if n == 1:
        return 1
    else:
        return n + sum_natural(n-1)

sum_natural(5)
15

Ֆիբոնաչի

1, 1, 2, 3, 5, 8, 13, 21, 34, 55

def fibonacci(n: int) -> int:
    print(f"fibonacci() called with n = {n}")
    if n == 2:
        return_val =  1
    else:
        return_val =  fibonacci(n-2) + fibonacci(n-1)
    print(f"-> fibonacci({n}) returns {return_val}")
    return return_val
    
fibonacci(4)

# fibonacci(4-2) -> f(2)

1 + f(4-1)
def fibonacci(n: int) -> int:
    assert n > 0, "n must be a positive integer"
    print(f"fibonacci() called with n = {n}")
    
    if n == 1:
        return_val = 1
    elif n == 2:
        return_val = 1
    else:
        return_val = fibonacci(n-2) + fibonacci(n-1)
    print(f"-> fibonacci({n}) returns {return_val}")
    return return_val
    
fibonacci(-3)

# fibonacci(4-2) -> f(2)

# 1 + f(4-1)
# 1 1 2 3
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

Input In [22], in <cell line: 14>()

     11     print(f"-> fibonacci({n}) returns {return_val}")

     12     return return_val

---> 14 fibonacci(-3)

     16 # fibonacci(4-2) -> f(2)

     17 

     18 # 1 + f(4-1)

     19 # 1 1 2 3



Input In [22], in fibonacci(n)

      1 def fibonacci(n: int) -> int:

----> 2     assert n > 0, "n must be a positive integer"

      4     print(f"fibonacci() called with n = {n}")

      5     if n == 1:



AssertionError: n must be a positive integer

# 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

def fibonacci(n):
    if n == 1:
        return 1
    elif n == 2:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)
fibonacci(4)
3

🛠️ Գործնական

🏡Տնային

🎲 12

Flag Counter