Code Monkey home page Code Monkey logo

Comments (3)

bluecoconut avatar bluecoconut commented on May 12, 2024 1

Hey @deepakmehta79, you can absolutely do this pretty easily, but i didn't build it directly in due to the potential risk of calling exec.

Here's an example

import pandas as pd
import sketch
import random

df = pd.DataFrame(
    {   
        "transaction_id": [random.randint(1, 5121) for _ in range(20)],
        "fruit": [random.choice(["Orange", "Apple", "Banana"]) for _ in range(20)],
        "price": [random.randint(1, 323)/100 for _ in range(20)],
    }
)

code_to_run = df.sketch.howto("find the average price and standard deviation for each fruit and print it out in a nice way", call_display=False)
exec(code_to_run)

Note 2 things,
(1) I use call_display on the sketch.howto, this means that it will return a string i can store into a variable.
(2) I execute that code with exec

The reason i avoided including this as the default behavior was due to security implications of running code that you get generated from a model (who knows what it could do!) and also for transparency of process.

Rather than simply assuming the AI is right, the extra copy-paste step helps bring transparency into what is happening, so any data-results can be defended!

That said, I completely understand the desire to expidite the process when you're just hacking away, so i'd suggest something like that.

Also, if you want a "step up", here's a fun snippet i just threw together:

@pd.api.extensions.register_dataframe_accessor("run")
class SketchHelper:
    def __init__(self, pandas_obj):
        self._obj = pandas_obj

    def __call__(self, command):
        code_to_run = self._obj.sketch.howto(command, call_display=False)
        exec(code_to_run)

followed by:

df.run("find the average price and standard deviation for each fruit and print it out in a nice way")

which works!~

Use this power at your own discretion!

from sketch.

deepakmehta79 avatar deepakmehta79 commented on May 12, 2024

Hi @bluecoconut, thank you so much, this is indeed super cool.
I am trying to following your suggesting, and trying to embed in the streamlit app but I am getting the following error:

RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.
Traceback:
File "/opt/conda/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.dict)

import sketch
File "/opt/conda/lib/python3.10/site-packages/sketch/init.py", line 2, in
from .pandas_extension import SketchHelper # noqa
File "/opt/conda/lib/python3.10/site-packages/sketch/pandas_extension.py", line 16, in
import lambdaprompt
File "/opt/conda/lib/python3.10/site-packages/lambdaprompt/init.py", line 12, in
nest_asyncio.apply()
File "/opt/conda/lib/python3.10/site-packages/nest_asyncio.py", line 18, in apply
loop = loop or asyncio.get_event_loop()
File "/opt/conda/lib/python3.10/site-packages/nest_asyncio.py", line 45, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File "/opt/conda/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'

from sketch.

bluecoconut avatar bluecoconut commented on May 12, 2024

Hey @deepakmehta79 ,

This seems to be a conflict in nest_asyncio and streamlit

the fix seems to be adding a few lines to the top of the file.

import asyncio

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

If you add this before import streamlit as st then things should work.

from sketch.

Related Issues (20)

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.