Skip to content

Instantly share code, notes, and snippets.

@generalistcodes
Forked from jeremyjordan/button_example.md
Created September 21, 2020 23:15
Show Gist options
  • Select an option

  • Save generalistcodes/19eb6d66985b1ff44d2d4c82cf53c82b to your computer and use it in GitHub Desktop.

Select an option

Save generalistcodes/19eb6d66985b1ff44d2d4c82cf53c82b to your computer and use it in GitHub Desktop.

Revisions

  1. @jeremyjordan jeremyjordan renamed this gist Oct 27, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jeremyjordan jeremyjordan renamed this gist Oct 27, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @jeremyjordan jeremyjordan revised this gist Oct 27, 2019. No changes.
  4. @jeremyjordan jeremyjordan created this gist Oct 27, 2019.
    18 changes: 18 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    Example for discussion: https://discuss.streamlit.io/t/how-to-evolve-complex-state-e-g-annotate-data/

    Install the requirements.
    ```
    fastapi==0.42.0
    streamlit==0.49.0
    uvicorn==0.9.1
    ```

    Run the server.
    ```
    uvicorn server:app --reload
    ```

    Run Streamlit.
    ```
    streamlit run letters.py
    ```
    19 changes: 19 additions & 0 deletions letters.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import streamlit as st
    import requests

    st.subheader('existing letters')
    letters = requests.get('http://127.0.0.1:8000/letters').json()
    for letter in letters:
    st.write(letter)

    st.subheader('add new letters')
    new_candidates = []
    for new_letter in ['d', 'e', 'f']:
    st.write(f'add {new_letter}')
    add_letter = st.checkbox(f'add {new_letter}')
    if add_letter:
    new_candidates.append(new_letter)

    submit = st.button('submit new letters')
    if submit:
    requests.post('http://127.0.0.1:8000/letters', json=new_candidates)
    17 changes: 17 additions & 0 deletions server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    from fastapi import FastAPI
    from typing import List

    app = FastAPI()

    LETTERS = []


    @app.get("/letters")
    def list_letters():
    return LETTERS


    @app.post("/letters")
    def add_letters(letters: List[str]):
    LETTERS.extend(letters)
    return {'added': len(letters)}