Issue
I've the following code running in my jupyter notebook:
import pandas as pd
class intData:
def __init__(self, inputPath, fileName):
self.inputPath = inputPath
self.filename = filename
def dataProcess(self, colNames, NewColnames, NewColnamesLog, val):
df = pd.read_csv(self.inputPath + self.filename)
df = df[colNames]
for i, j, k in list(zip(colNames, NewColnames, val)):
df[j] = df[i] + k
return df
finalDataProcess = intData(filepath, filename)
finalDataProcess = finalDataProcess.dataProcess(['A', 'B', 'C'], ['newA','newB','newC'], [2, 3, 4])
In this code, filepath
, filename
, colnames
, newColnames
, and val
will change. So, I want to make it interactive. There will be drop down menu with list of filepath
, filename
, colnames
, newColnames
, and val
(separately). And when we select them, we get the final output. This output will then be exported.
Can someone please guide me how to approach this problem? I would prefer it to be in jupyter notebook. But if it's not possible, then other python option will be fine.
Thanks!
Solution
There's some good options to help you with this!
For a while, the canonical library to create mini reactive interfaces from your functions in your Jupyter notebooks has been ipywidgets and is still a great option. You can also use Voila to convert notebooks containing these interfaces into dashboard web apps.
A more recent option, that allows you to make complex interfaces in your notebooks and even standalone dashboards is Panel.
There is also Plotly Dash, which, while being geared more towards making standalone web apps that feel a bit closer to working with the modern web platform, has the jupyter-dash library that supports use in Jupyter Notebooks.
My advice would be to start with ipywidgets (maybe using Voila as well). If that doesn't do what you want, then try Panel or Dash.
Here's an example of how you can use ipywidgets
with a class similar to yours:
from ipywidgets import interact
import pandas as pd
class Foo:
def __init__(self, a, b):
self.a = a
self.b = b
def make_df(self, length):
df = pd.DataFrame({"a":[self.a]*length, "b":[self.b]*length})
return df
@interact(a=[1,2,3], b=["x", "y", "z"], length=range(1,11))
def helper(a, b, length):
foo = Foo(a, b)
df = foo.make_df(length)
return df
Answered By - nedned
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.