<jupyter_start><jupyter_text>CityBike Usage Live Feed This URL points to a JSON endpoint that contains information about every CitiBike station in New York. It updates every 10 seconds, and contains such information as  of ebikes available,  of regular bikes available,  of open docks available, etc.<jupyter_code>STATION_INFORMATION_URL = 'https://gbfs.citibikenyc.com/gbfs/en/station_status.json'<jupyter_output><empty_output><jupyter_text>Control how often the REST endpoint should be queried, keeping in mind that new data is only available every 10 seconds.<jupyter_code>REPEAT_RATE = 5
import aiohttp
import asyncio
import json
import datetime as dt
import pandas as pd
import altair as alt
async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()
async def load_data():
    async with aiohttp.ClientSession() as session:
        raw_json = await fetch(session, STATION_INFORMATION_URL)
        return json.loads(raw_json)
async def transform_data_iter():
    while True:
        bike_data = await load_data()
        total_available_bikes = sum(map(lambda s: s['num_bikes_available'],
                                        bike_data['data']['stations']))
        yield {'total_bikes': total_available_bikes, 'time': dt.datetime.now()}
        
        await asyncio.sleep(REPEAT_RATE)
def plot(full_data, baseline):
    df = pd.DataFrame(full_data, columns=['time', 'total_bikes'])
    
    chart = alt.Chart(df).mark_line().encode(
        x=alt.X('time:T', axis=alt.Axis(title='Time')),
        y=alt.Y('total_bikes:Q',
                axis=alt.Axis(title='Total Available Bikes'),
                scale=alt.Scale(zero=False))
    ).configure_view(
        width=600,
    ).properties(
        title='Total Available CitiBikes in New York'
    )
    
    return chart
full_data_feed = list()
bike_data_item = transform_data_iter()<jupyter_output><empty_output><jupyter_text>Realtime Plot<jupyter_code># This line introduces the data dependency of this cell on the data generating cell above
full_data_feed.append(bike_data_item)

print(f"{len(full_data_feed)} items")

plot(full_data_feed, full_data_feed[0]['total_bikes'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0972972972972973, 'fraction_numerical': 0.004054054054054054, 'mean_word_length': 4.036281179138322, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '582740', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 696, 'n_tokens_neox': 682, 'n_words': 181, 'path': 'examples/CitiBikeUsage.ipynb', 'repo_name': 'gnestor/reactivepy', 'size': '2220'}