srush / streambook

Live Python Notebooks with any Editor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CLI and minor improvement

cgarciae opened this issue · comments

Hey Sasha!

I am creating a notebook / future blog article and everything has worked very well, I usually create scripts because I don't like notebooks very much but streambook has been the perfect mix, streamlit itself is quite nice to use while developing. If its ok with you I wanted to contribute some quality-of-life improvements to the project:

  • Creating a CLI using typer, typer much more expressive and easier to use than plain argparse. We could have these 2 commands:
    • streambook run [file] which would be equivalent to python -m streambook
    • streambook export [input-file] which would be equivalent to python -m streambook [input-file] --nowatch but maybe without the prints and an optional --output-file flag.
  • Use poetry to develop. This is very optional but poetry is a modern way to both setup the environment for a project since dependencies are maintained in a .lock file with exact version hashes, and develop a python package since deploying to pypi is super easy. You can expose the streambook script using poetry, else this can be done using setup.py.

Sounds great! Haven't used either, but wouldn't mind learning.

I've been using it to blog as well, and it is surprisingly pleasant. I particularly like that I can use black and lint tools. We should promote it more.

I'll try to add some new feature issues as well. A couple that came up in my use:

  • It should print out the port number when it runs and make it easy to give it in the CLI.
  • You should be able to mask lines that are not included in the python output, if you want to write a library (something like if name == ...)
  • Caching is really important and should be documented better.
  • Matplotlib is a bit annoying to use and should be documented. You have to manually create a plt.Figure().
  • Ideally the notebook would export the virtual environment somehow. I think this is possible but I don't know how. (not a fan of the ! magic).
  • Printing is kind of buggy.
  • When it throws an error, it doesn't show the code that failed and the line numbers are obviously not correct.

It should print out the port number when it runs and make it easy to give it in the CLI.

Definitely a good idea, it was confusing to me at first.

You should be able to mask lines that are not included in the python output, if you want to write a library (something like if name == ...)

I have been adding # __st comments to lines I want to remove from the python output:

fig = plt.figure()  # __st
plt.plot(x, y)
plt.show()
fig  # __st

A bit hacky but it works, a standardized approach would be nice.

Printing is kind of buggy.

Maybe automatically replace print(...) with __st.write(...)? I've been using write instead of print for quick debugging.

When it throws an error, it doesn't show the code that failed and the line numbers are obviously not correct.

I've resorted to debugging the {file_name}.streambook.py file directly using streamlit but seems tricky to solve this.

Oh,. these are all good idea... Maybe if the last line in a block is non indented and a non-assignment it should also wrap it in __st.write()

df[df["col"] == 2]

becomes

__st.write(df[df["col"] == 2])

which mimics the notebook behavior.

I've been pretty hesitant to manipulate code directly, but I think with the Python AST class this would not be so hard to do. (Streamlit must be doing it).