Skip to content

Commit

Permalink
Merge pull request #15 from clitic/pyo3
Browse files Browse the repository at this point in the history
Added support for jupyter notebook
  • Loading branch information
clitic authored Oct 5, 2023
2 parents 22bfbbd + 822b373 commit 2e9511f
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 13 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run Tests
run: cargo test --verbose --all-features
- name: Run Tests # https://pyo3.rs/faq
run: cargo test -p kdam -p kdam_derive --all-features
- name: Build file_download example
run: cargo build -p file_download
- name: Build notebook example
run: cargo build -p notebook
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Cargo.lock
**/*.rs.bk
kdam-logs.txt
kdam/kdam-logs.txt
kdam/examples/notebook/.ipynb_checkpoints
kdam/examples/notebook/kdam_notebook*
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[workspace]
members = [
"kdam",
"kdam/examples/notebook",
"kdam/examples/miscellaneous/file_download",
"kdam_derive",
]
resolver = "2"
9 changes: 8 additions & 1 deletion kdam/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.5.0] - 2023-10-05

## Added

- New `notebook` feature.

## [0.4.1] - 2023-08-22

### Fixed
Expand Down Expand Up @@ -185,7 +191,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.1.0] - 2022-03-20

[Unreleased]: https://github.com/clitic/kdam/compare/v0.4.1...HEAD
[Unreleased]: https://github.com/clitic/kdam/compare/v0.5.0...HEAD
[0.5.0]: https://github.com/clitic/kdam/compare/v0.4.1...v0.5.0
[0.4.1]: https://github.com/clitic/kdam/compare/v0.4.0...v0.4.1
[0.4.0]: https://github.com/clitic/kdam/compare/v0.3.0...v0.4.0
[0.3.0]: https://github.com/clitic/kdam/compare/799b34c...v0.3.0
Expand Down
8 changes: 5 additions & 3 deletions kdam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ license = "MIT OR Apache-2.0"
name = "kdam"
readme = "README.md"
repository = "https://github.com/clitic/kdam/tree/main/kdam"
version = "0.4.1"
version = "0.5.0"

[dependencies]
colorgrad = { version = "0.6", optional = true }
formatx = { version = "0.2.1", optional = true }
kdam_derive = { version = "0.1.0", path = "../kdam_derive", optional = true }
rayon = { version = "1.7", optional = true }
terminal_size = "0.2"
pyo3 = { version = "0.19", optional = true }
rayon = { version = "1.8", optional = true }
terminal_size = "0.3"
unicode-segmentation = { version = "1", optional = true }
unicode-width = { version = "0.1", optional = true }

Expand All @@ -26,6 +27,7 @@ windows-sys = { version = "0.48.0", features = ["Win32_Foundation", "Win32_Syste
[features]
derive = ["dep:kdam_derive"]
gradient = ["dep:colorgrad"]
notebook = ["dep:pyo3"]
rayon = ["dep:rayon"]
rich = ["derive"]
spinner = []
Expand Down
3 changes: 2 additions & 1 deletion kdam/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ A console progress bar library for Rust. (inspired by [tqdm](https://github.com/
The following are a list of [Cargo features](https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section) that can be enabled or disabled:

- **derive**: Adds a derive macro for deriving [BarExt](crate::BarExt) trait.
- **notebook**: Enables support for jupyter notebooks. It can be useful when you are building [pyo3](https://crates.io/crates/pyo3) python extension modules. Also, make sure that you have [ipython](https://pypi.org/project/ipython) and [ipywidgets](https://pypi.org/project/ipywidgets) python libraries installed on your system.
- **gradient**: Enables gradient colours support for progress bars and printing text.
- **rayon**: Adds supports for [rayon's](https://crates.io/crates/rayon) iterators.
- **rayon**: Adds support for [rayon's](https://crates.io/crates/rayon) parallel iterators.
- **rich**: Enables [rich](https://rich.readthedocs.io/en/latest/progress.html) style progress bar.
- **spinner**: Enables support for using spinners.
- **template**: Enables templating capabilities for [Bar](crate::Bar).
Expand Down
4 changes: 2 additions & 2 deletions kdam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</a>
</p>

kdam is a console progress bar library for rust. It is port of [tqdm](https://github.com/tqdm/tqdm) library which is written in python. kdam supports almost all features of tqdm except few. Some features of tqdm can't be ported directly. So they are implemented in different way like, [RowManager](https://docs.rs/kdam/latest/kdam/struct.RowManager.html) which manages multiple progress bars but in tqdm progress bars are automatically managed using `nrows`. In addition to tqdm existing features kdam also provides extra features such as spinners, charset with fill, gradient colours etc. Since, kdam is written in rust its upto 4 times faster than tqdm.
kdam is a console progress bar library for rust. It is port of [tqdm](https://github.com/tqdm/tqdm) library which is written in python. kdam supports almost all features of tqdm except a few. It also supports [jupyter notebook](https://github.com/clitic/kdam/tree/main/kdam/examples/notebook) same as tqdm does. Some features of tqdm can't be ported directly. So they are implemented in different way like, [RowManager](https://docs.rs/kdam/latest/kdam/struct.RowManager.html) which manages multiple progress bars but in tqdm progress bars are automatically managed using `nrows`. In addition to tqdm existing features kdam also provides extra features such as spinners, charset with fill, gradient colours etc. Since, kdam is written in rust its upto 4 times faster than tqdm.

Instantly make your loops show a smart progress meter. Just wrap any iterator with tqdm!(iterator) macro and you're done!

Expand Down Expand Up @@ -65,7 +65,7 @@ Add this to your Cargo.toml file.

```toml
[dependencies]
kdam = "0.4.1"
kdam = "0.5.0"
```

Or add from command line.
Expand Down
2 changes: 1 addition & 1 deletion kdam/examples/miscellaneous/file_download/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

pb.refresh()?;
eprintln!("\nDownloaded {} to {}", URL, PATH);
eprintln!("\nDownloaded {}", PATH);
Ok(())
}
15 changes: 15 additions & 0 deletions kdam/examples/notebook/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "notebook"
edition = "2021"
version = "0.1.0"

[lib]
name = "kdam_notebook"
crate-type = ["cdylib"]

[dependencies]
kdam = { path = "../..", features = ["notebook", "template"] }
pyo3 = { version = "0.19", features = ["extension-module"] }

[build-dependencies]
pyo3-build-config = "0.19"
3 changes: 3 additions & 0 deletions kdam/examples/notebook/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
pyo3_build_config::add_extension_module_link_args();
}
79 changes: 79 additions & 0 deletions kdam/examples/notebook/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# cargo build -p notebook\n",
"# cp target/debug/kdam_notebook.dll kdam/examples/notebook/kdam_notebook.pyd\n",
"# cp target/debug/libkdam_notebook.so kdam_notebook.so\n",
"import kdam_notebook\n",
"kdam_notebook.set_notebook(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"kdam_notebook.progress_bar()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"kdam_notebook.progress_bar_with_template()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from time import sleep\n",
"from tqdm.notebook import tqdm\n",
"\n",
"pb = tqdm(total=300, mininterval=0.0)\n",
"\n",
"for _ in range(300):\n",
" sleep(0.02)\n",
" pb.update(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
44 changes: 44 additions & 0 deletions kdam/examples/notebook/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use kdam::{tqdm, BarExt};
use pyo3::prelude::*;

#[pyfunction]
fn set_notebook(running: bool) -> PyResult<()> {
kdam::set_notebook(running);
Ok(())
}

#[pyfunction]
fn progress_bar() -> PyResult<()> {
let mut pb = tqdm!(total = 300, force_refresh = true);

for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.02));
pb.update(1).unwrap();
}

Ok(())
}

#[pyfunction]
fn progress_bar_with_template() -> PyResult<()> {
let mut pb = tqdm!(
total = 300,
force_refresh = true,
bar_format = "{animation} {percentage:.2}% @ {rate:.1} it/s"
);

for _ in 0..300 {
std::thread::sleep(std::time::Duration::from_secs_f32(0.02));
pb.update(1).unwrap();
}

Ok(())
}

#[pymodule]
fn kdam_notebook(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(set_notebook, m)?)?;
m.add_function(wrap_pyfunction!(progress_bar, m)?)?;
m.add_function(wrap_pyfunction!(progress_bar_with_template, m)?)?;
Ok(())
}
4 changes: 4 additions & 0 deletions kdam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub use crate::std::{
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
pub use kdam_derive::BarExt;

#[cfg(feature = "notebook")]
#[cfg_attr(docsrs, doc(cfg(feature = "notebook")))]
pub use crate::std::set_notebook;

#[cfg(feature = "rayon")]
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))]
pub use rayon;
Expand Down
Loading

0 comments on commit 2e9511f

Please sign in to comment.