Code Monkey home page Code Monkey logo

easycharts's Introduction

Easily create data visualization of static or streaming data

Get Started

pip install easycharts

Create EasyCharts Server

# charts.py
from fastapi import FastAPI
from easycharts import ChartServer

server = FastAPI()

@server.on_event('startup')
async def setup():
    server.charts = await ChartServer.create(
        server,
        charts_db="test"
    )

    await server.charts.create_dataset(
        "test",
        labels=['a', 'b', 'c', 'd'],
        dataset=[1,2,3,4]
    )

Start Server

uvicorn --host 0.0.0.0 --port 0.0.0.0 charts:server

Update Data via API

In a separate window, access the OpenAPI docs to demonstrate dynanimc updates to the graph

http://0.0.0.0:8220/docs

Line

Bar

APIS

Real World Usage - Resource Monitoring

import datetime, psutil
import asyncio
from fastapi import FastAPI
from easycharts import ChartServer
from easyschedule import EasyScheduler

scheduler = EasyScheduler()
server = FastAPI()

every_minute = '* * * * *'

@server.on_event('startup')
async def setup():
    asyncio.create_task(scheduler.start())
    server.charts = await ChartServer.create(
        server,
        charts_db="charts_database",
        chart_prefix = '/mycharts'
    )

    await server.charts.create_dataset(
        "test",
        labels=['a', 'b', 'c', 'd'],
        dataset=[1,2,3,4]
    )

    # set initial sync time
    label=datetime.datetime.now().isoformat()[11:19]
    await server.charts.create_dataset(
        'cpu',
        labels=[label],
        dataset=[psutil.cpu_percent()]
    )
    await server.charts.create_dataset(
        'mem',
        labels=[label],
        dataset=[psutil.virtual_memory().percent]
    )

    @scheduler(schedule=every_minute)
    async def resource_monitor():
        time_now=datetime.datetime.now().isoformat()[11:19]

        # updates CPU & MEM datasets with current time
        await server.charts.update_dataset(
            'cpu',
            label=time_now,
            data=psutil.cpu_percent()
        )
        await server.charts.update_dataset(
            'mem',
            label=time_now,
            data=psutil.virtual_memory().percent
        )

easycharts's People

Contributors

codemation avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

easycharts's Issues

KeyError: 'serialization'

I get an error when trying to run the example:

10-06 12:54:36 uvicorn.error INFO     connection open
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/miniconda/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 203, in run_asgi
    result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
  File "/miniconda/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/middleware/errors.py", line 146, in __call__
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/exceptions.py", line 58, in __call__
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 68, in app
    await func(session)
  File "/miniconda/lib/python3.8/site-packages/easyrpc/server.py", line 250, in origin
    serialization = setup['serialization']
KeyError: 'serialization'
10-06 12:54:36 uvicorn.error ERROR    Exception in ASGI application
Traceback (most recent call last):
  File "/miniconda/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 203, in run_asgi
    result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
  File "/miniconda/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/middleware/errors.py", line 146, in __call__
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/exceptions.py", line 58, in __call__
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/miniconda/lib/python3.8/site-packages/starlette/routing.py", line 68, in app
    await func(session)
  File "/miniconda/lib/python3.8/site-packages/easyrpc/server.py", line 250, in origin
    serialization = setup['serialization']
KeyError: 'serialization'
INFO:     connection closed

I created a new conda env and just installed the depenencies

real world example fails to launch

Hi I tried running the real world example you have but I'm getting the following error:

`PS C:\Code\fastapiBench> uvicorn --host 0.0.0.0 --port 6969 charts:server
INFO: Started server process [19284]
08-19 00:39:41 uvicorn.error INFO Started server process [19284]
INFO: Waiting for application startup.
08-19 00:39:41 uvicorn.error INFO Waiting for application startup.
08-19 00:39:41 aiopyql-db-charts_database.db ERROR error during create_table - KeyError(1660865981.261195)
Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 565, in create_table
result = await new_table.create_schema()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 63, in create_schema
return await self.database.run(await self.get_schema())
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 447, in run
self.cache_check(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 294, in cache_check
for cache, _ in self.cache:
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\cache.py", line 37, in cache_generator
yield cache_key, self.timestamp_to_cache[timestamp][0]
KeyError: 1660865981.261195
08-19 00:39:41 aiopyql-db-charts_database.db ERROR error during create_table - KeyError(1660865981.261195)
Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 565, in create_table
result = await new_table.create_schema()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 63, in create_schema
return await self.database.run(await self.get_schema())
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 447, in run
self.cache_check(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 294, in cache_check
for cache, _ in self.cache:
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\cache.py", line 37, in cache_generator
yield cache_key, self.timestamp_to_cache[timestamp][0]
KeyError: 1660865981.261195
08-19 00:39:41 aiopyql-db-charts_database.db ERROR exception inserting into test
Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 445, in insert
result = await self.database.run(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 447, in run
self.cache_check(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 294, in cache_check
for cache, _ in self.cache:
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\cache.py", line 37, in cache_generator
yield cache_key, self.timestamp_to_cache[timestamp][0]
KeyError: 1660865981.261195
ERROR: Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 635, in lifespan
async with self.lifespan_context(app):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 530, in aenter
await self._router.startup()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 612, in startup
await handler()
File "C:\Code\fastapiBench.\charts.py", line 21, in setup
await server.charts.create_dataset(
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\easycharts\charts.py", line 185, in create_dataset
await self.db.tables[name].insert(
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 452, in insert
raise e
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 445, in insert
result = await self.database.run(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 447, in run
self.cache_check(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 294, in cache_check
for cache, _ in self.cache:
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\cache.py", line 37, in cache_generator
yield cache_key, self.timestamp_to_cache[timestamp][0]
KeyError: 1660865981.261195

08-19 00:39:41 uvicorn.error ERROR Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 635, in lifespan
async with self.lifespan_context(app):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 530, in aenter
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 612, in startup
await handler()
File "C:\Code\fastapiBench.\charts.py", line 21, in setup
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\easycharts\charts.py", line 185, in create_dataset
await self.db.tables[name].insert(
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 452, in insert
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\table.py", line 445, in insert
result = await self.database.run(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 447, in run
self.cache_check(query)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\data.py", line 294, in cache_check
for cache, _ in self.cache:
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\aiopyql\cache.py", line 37, in cache_generator
yield cache_key, self.timestamp_to_cache[timestamp][0]
KeyError: 1660865981.261195

ERROR: Application startup failed. Exiting.
08-19 00:39:41 uvicorn.error ERROR Application startup failed. Exiting.
PS C:\Code\fastapiBench> & C:/Users/mac/AppData/Local/Programs/Python/Python310/python.exe c:/Code/fastapiBench/charts.py
File "c:\Code\fastapiBench\charts.py", line 21
await server.charts.create_dataset(
IndentationError: unexpected indent
PS C:\Code\fastapiBench> & C:/Users/mac/AppData/Local/Programs/Python/Python310/python.exe c:/Code/fastapiBench/charts.py
File "c:\Code\fastapiBench\charts.py", line 21
await server.charts.create_dataset(
IndentationError: unexpected indent
PS C:\Code\fastapiBench> uvicorn --host 0.0.0.0 --port 6969 charts:server
Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\Scripts\uvicorn.exe_main
.py", line 7, in
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1130, in call
return self.main(*args, **kwargs)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\click\core.py", line 760, in invoke
return _callback(*args, **kwargs)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\main.py", line 407, in main
run(
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\main.py", line 575, in run
server.run()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
return future.result()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\server.py", line 67, in serve
config.load()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\config.py", line 479, in load
self.loaded_app = import_from_string(self.app)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\importlib_init
.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 879, in exec_module
File "", line 1017, in get_code
File "", line 947, in source_to_code
File "", line 241, in _call_with_frames_removed
File "C:\Code\fastapiBench.\charts.py", line 21
await server.charts.create_dataset(
IndentationError: unexpected indent
PS C:\Code\fastapiBench> uvicorn --host 0.0.0.0 --port 6969 charts:server
INFO: Started server process [2640]
08-19 00:40:32 uvicorn.error INFO Started server process [2640]
INFO: Waiting for application startup.
08-19 00:40:32 uvicorn.error INFO Waiting for application startup.
ERROR: Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 635, in lifespan
async with self.lifespan_context(app):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 530, in aenter
await self._router.startup()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 612, in startup
await handler()
File "C:\Code\fastapiBench.\charts.py", line 15, in setup
await server.charts.create_dataset(
AttributeError: 'FastAPI' object has no attribute 'charts'

08-19 00:40:32 uvicorn.error ERROR Traceback (most recent call last):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 635, in lifespan
async with self.lifespan_context(app):
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 530, in aenter
await self._router.startup()
File "C:\Users\mac\AppData\Local\Programs\Python\Python310\lib\site-packages\starlette\routing.py", line 612, in startup
await handler()
File "C:\Code\fastapiBench.\charts.py", line 15, in setup
await server.charts.create_dataset(
AttributeError: 'FastAPI' object has no attribute 'charts'

ERROR: Application startup failed. Exiting.
08-19 00:40:32 uvicorn.error ERROR Application startup failed. Exiting.`

Using:

uvicorn --host 0.0.0.0 --port 6969 charts:server

As the command to run it as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.