webrust 1.5.0

Python-like Rust for Web Applications - A bridge between Python simplicity and Rust power
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# WebRust: A Unified Framework for Data Visualization and Interactive Computing

[![WebRust](https://img.shields.io/badge/WebRust-1.5.0-ff6b35?style=flat-square)](https://github.com/gerarddubard/webrust)
[![Rust](https://img.shields.io/badge/Rust-1.70+-000?style=flat-square&logo=rust)](https://rust-lang.org)
[![Documentation](https://img.shields.io/badge/docs-latest-blue?style=flat-square)](https://docs.rs/webrust)
[![License](https://img.shields.io/badge/license-MIT-green?style=flat-square)](LICENSE)

**Links:** [Documentation]https://docs.rs/webrust | [Examples]https://github.com/gerarddubard/webrust/tree/main/examples | [Crates.io]https://crates.io/crates/webrust

---

## Abstract

WebRust is a Rust framework designed to bridge the ergonomics of Python with the performance characteristics of Rust, while providing integrated web-based visualization capabilities. The framework addresses the fragmentation in contemporary data analysis workflows by offering a unified interface for data manipulation, visualization, and interactive application development. Version 1.5.0 introduces an optional SQL analytics layer and optimized compilation times.

## Table of Contents

1. [Introduction]#introduction
2. [Motivation]#motivation
3. [Architecture]#architecture
4. [Installation]#installation
5. [Core Features]#core-features
6. [Performance Characteristics]#performance-characteristics
7. [Usage Examples]#usage-examples
8. [Use Cases]#use-cases
9. [Roadmap]#roadmap
10. [Contributing]#contributing
11. [License]#license

---

## Introduction

### Overview

WebRust is a framework that combines Python-inspired syntax patterns with Rust's type safety and performance characteristics. The primary objective is to reduce the complexity of creating interactive, web-based data visualizations and applications while maintaining compile-time guarantees and native execution speeds.

### Key Characteristics

- **Ergonomic Syntax**: Python-like iterator patterns and comprehensions
- **Type Safety**: Full Rust type system integration
- **Web Integration**: Automatic browser-based UI generation
- **Zero Configuration**: No external dependencies for core functionality
- **Optional SQL**: DuckDB integration for analytical workloads (opt-in)

### Version 1.5.0 Highlights

- Modular SQL support (opt-in via feature flag)
- Reduced default compilation time: approximately 30 seconds (from 5-10 minutes)
- Performance optimizations: 40-60% improvement in rendering
- Enhanced memory efficiency: 60% reduction in allocation overhead

---

## Motivation

### Problem Statement

Contemporary data analysis and visualization workflows typically require multiple tools and languages:

1. **Data Retrieval**: SQL databases (PostgreSQL, MySQL)
2. **Data Processing**: Python with pandas/numpy
3. **Visualization**: matplotlib, plotly, or similar libraries
4. **Web Deployment**: Flask, Django, or JavaScript frameworks

This fragmentation results in:

- Multiple context switches between languages and tools
- Complex dependency management
- Data format conversion overhead
- Extended development cycles
- Infrastructure complexity for deployment

### Existing Approaches and Limitations

#### Terminal-Based Applications

Traditional command-line interfaces lack support for:

- Rich text formatting and colors
- Embedded visualizations
- Mathematical notation
- Interactive elements

#### Traditional Data Analysis Pipelines

Multi-tool workflows involving:

- SQL for queries
- Python for processing
- Separate visualization libraries
- Web frameworks for deployment

Result in high complexity and slow iteration cycles.

#### Web Framework Solutions

Frameworks like Rocket or Actix-web require:

- Multiple language expertise (HTML/CSS/JavaScript)
- Separate frontend/backend logic
- Complex state management
- Deployment infrastructure

#### Desktop GUI Frameworks

Native GUI frameworks (egui, iced) present challenges:

- Framework-specific API learning curve
- Platform-specific considerations
- Distribution complexity
- Update deployment overhead

### Design Philosophy

WebRust proposes a unified approach based on three principles:

1. **Syntax Evolution**: Adopting ergonomic patterns without sacrificing performance
2. **Ecosystem Integration**: Learning from Python, Rust, and SQL communities
3. **Modern Defaults**: Prioritizing visual, interactive, and zero-configuration solutions

---

## Architecture

### System Design

WebRust consists of three primary layers:

1. **Syntax Layer**: Macro-based transformations for Python-like constructs
2. **Runtime Layer**: HTTP server and browser communication
3. **Visualization Layer**: Integration with ECharts, MathJax, and Two.js

### Compilation Model

```text
Source Code → Macro Expansion → Type Checking → Native Compilation
     ↓              ↓                ↓                ↓
Python-like    Standard Rust    Type Safety    Native Performance
  Syntax        Iterators       Guaranteed      (no runtime cost)
```

### Optional SQL Integration

When enabled via `features = ["sql"]`:

- **Engine**: DuckDB (in-memory OLAP)
- **Data Format**: Apache Arrow (columnar)
- **Compilation**: 2-5 minutes (first build)
- **Capabilities**: Standard SQL, window functions, CTEs

---

## Installation

### Prerequisites

- Rust 1.70 or later
- Cargo package manager

### Basic Installation

For standard features (recommended):

```toml
[dependencies]
webrust = "1.5.0"
```

**Characteristics:**

- Compilation time: approximately 30 seconds
- Size: minimal
- Features: Python-like syntax, web GUI, charts, tables, LaTeX rendering, turtle graphics

### With SQL Analytics

For data-intensive applications:

```toml
[dependencies]
webrust = { version = "1.5.0", features = ["sql"] }
```

**Additional characteristics:**

- First compilation: 2-5 minutes (due to DuckDB)
- Subsequent builds: cached and faster
- Additional features: DuckDB integration, SQL queries, Arrow streaming

---

## Core Features

### 1. Iterator Extensions

Python-style range construction and iteration:

```rust,ignore
use webrust::prelude::*;

// Range iteration
for i in 0.to(10) {
    println!("{i}");
}

// Step specification
for i in 0.to(100).by(5) {
    println!("{i}");
}

// Character ranges
for c in 'a'.to('z') {
    println!("{c}");
}

// Floating-point and negative steps
for x in 4.0.to(0.0).by(-0.5) {
    println!("{x}");
}
```

### 2. Comprehension Patterns

```rust,ignore
use webrust::prelude::*;
use std::collections::HashMap;

// Map transformation
let squares: Vec<i32> = 0.to(10).then(|x| x * x);

// Filter and transform
let evens: Vec<i32> = 0.to(20)
    .when(|&x| x % 2 == 0)
    .then(|x| x);

// Dictionary construction
let dict: HashMap<i32, i32> = 0.to(5)
    .then(|x| (x, x * x));
```

**Implementation note**: All operations compile to standard Rust iterators with zero runtime overhead.

### 3. String Operations

```rust,ignore
use webrust::prelude::*;

// Splitting operations
let parts = "a,b,c".splitby(",");
let words = "hello  world".splitby("");  // Whitespace split
let lines = "L1\nL2\nL3".splitby("\n");

// Joining
let joined = parts.join(", ");

// Case transformations
let upper = "hello".upper();
let title = "hello world".title();
```

### 4. Formatted Output

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    let name = "Alice";
    let age = 30;
    let pi = std::f64::consts::PI;
    
    // Variable interpolation
    println!("Hello {name}, you are {age} years old");
    
    // Expressions
    println!("Next year: {age + 1}");
    
    // Format specifiers
    println!("PI approx {pi:.2}");
    
    // JSON serialization
    println!("Data: {my_struct:j}");
    
    // LaTeX rendering
    println!("$(E = mc^2)");
}
```

**Implementation**: Compile-time macro expansion with no runtime overhead.

### 5. Visualization Components

#### Charts

```rust,ignore
use webrust::prelude::*;
use std::collections::HashMap;

#[gui]
fn main() {
    // Bar chart
    let sales = HashMap::from([
        ("Q1", 120.0), ("Q2", 200.0),
        ("Q3", 150.0), ("Q4", 300.0)
    ]);
    chart(&sales, "bar")
        .title("Quarterly Sales")
        .color("#2ecc71");
    
    // Line chart
    let temps = vec![64.4, 67.1, 69.8, 72.5, 70.2];
    chart(&temps, "line")
        .title("Temperature Trend")
        .xlabels(vec!["Mon", "Tue", "Wed", "Thu", "Fri"]);
}
```

**Supported chart types**: line, bar, pie, doughnut, radar, area, scatter, gauge, funnel

#### Tables

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    let matrix = vec![vec![1, 2, 3], vec![4, 5, 6]];
    table(&matrix).header(["X", "Y", "Z"]);
    
    // LaTeX support in tables
    let physics = vec![
        ("Einstein", r"$(E = mc^2)"),
        ("Schrodinger", r"$(i\hbar\frac{\partial}{\partial t}\Psi = \hat{H}\Psi)"),
    ];
    table(&physics).header(["Scientist", "Equation"]);
}
```

#### Graphics and Animation

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    coord("cartesian");
    
    let turtle = object();
    turtle.color("blue").width(2.0);
    
    // Geometric drawing
    for _ in 0.to(4) {
        turtle.forward(100.0);
        turtle.right(90.0);
    }
    
    // Animation with easing
    turtle.rotate(360.0).ease("elasticOut");
    turtle.scale(1.5, 1.5).ease("sineInOut");
}
```

**Animation support**: 20+ easing functions (linear, sine, quad, elastic, bounce, back, expo)

### 6. SQL Integration (Optional)

When `features = ["sql"]` is enabled:

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    // Data loading
    query("CREATE TABLE sales AS SELECT * FROM read_csv_auto('sales.csv')");
    
    // Analytical queries
    query(r#"
        SELECT 
            product,
            SUM(amount) AS total_sales,
            COUNT(*) AS transactions
        FROM sales
        GROUP BY product
        ORDER BY total_sales DESC
        LIMIT 10
    "#);
    
    // Window functions
    query(r#"
        SELECT 
            product,
            quarter,
            revenue,
            SUM(revenue) OVER (PARTITION BY product) AS total,
            RANK() OVER (ORDER BY revenue DESC) AS rank
        FROM sales
        WHERE year = 2024
    "#);
}
```

**Capabilities:**

- Standard SQL support
- Built-in CSV/JSON readers
- Window functions and CTEs
- Schema introspection
- Arrow-based streaming for large datasets

---

## Performance Characteristics

### Compilation Time

| Configuration    | First Build       | Subsequent Builds  |
|------------------|-------------------|--------------------|
| Default (no SQL) | approx 30 seconds | approx 1-2 seconds |
| With SQL feature | 2-5 minutes       | approx 1-2 seconds |

### Runtime Performance

**Rendering optimizations (v1.5.0):**

- F-string transformation: approximately 0.85 microseconds per operation (43% improvement)
- Memory allocations: approximately 5 per transformation (67% reduction)
- Memory footprint: approximately 340 bytes per transformation (60% reduction)

**Techniques employed:**

- SIMD pattern matching via `memchr`
- Zero-copy optimization with `Cow<str>`
- Optimized number formatting (`itoa` for integers, `ryu` for floats)
- Direct buffer writing

**Result**: Maintains 60fps animation performance with instant feedback.

### Memory Efficiency

All Python-like syntax constructs compile to standard Rust iterators, resulting in:

- Zero runtime overhead
- Optimal memory usage
- Full compiler optimization applicability

---

## Usage Examples

### Basic Interactive Application

```rust,ignore
use webrust::prelude::*;

#[gui(bg="navy", fg="white", font="Courier New")]
fn main() {
    println!("@(cyan, bold, italic)Data Dashboard");
    
    let name: String = input("What's your name?");
    println!("Hello, {name}!");
    
    let data = vec![10.0, 20.0, 30.0, 40.0, 50.0];
    chart(&data, "line").title("Trend Analysis");
    
    let squares: Vec<i32> = 0.to(10).then(|x| x * x);
    table(&squares).header(["Index", "Square"]);
}
```

**Execution**: `cargo run` then browser opens automatically and UI renders

### Data Visualization

```rust,ignore
use webrust::prelude::*;
use std::collections::HashMap;

#[gui]
fn main() {
    let sales = HashMap::from([
        ("Q1", 120.0), ("Q2", 200.0),
        ("Q3", 150.0), ("Q4", 300.0)
    ]);
    
    chart(&sales, "bar").title("Quarterly Revenue");
    
    let growth = vec![100.0, 150.0, 180.0, 250.0];
    chart(&growth, "line").title("Growth Trend");
}
```

### Scientific Computing

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    coord("cartesian");
    
    // Projectile motion simulation
    let v0 = 50.0;
    let angle = 45.0_f64.to_radians();
    let g = 9.81;
    
    let trajectory: Vec<(f64, f64)> = (0..100)
        .then(|i| {
            let t = i as f64 * 0.1;
            let x = v0 * angle.cos() * t;
            let y = v0 * angle.sin() * t - 0.5 * g * t * t;
            (x, y.max(0.0))
        });
    
    let path = object();
    path.color("red").width(2.0);
    for (x, y) in trajectory {
        path.line(x - 1.0, y, x, y);
    }
    
    println!(r"$(y = v_0 \sin\theta \cdot t - \frac{1}{2}gt^2)");
}
```

### Data Analytics with SQL

Requires `features = ["sql"]`:

```rust,ignore
use webrust::prelude::*;

#[gui]
fn main() {
    query(r#"
        CREATE TABLE logs AS 
        SELECT * FROM read_csv_auto('access_logs.csv');
        
        SELECT 
            DATE_TRUNC('hour', timestamp) AS hour,
            COUNT(*) AS requests,
            SUM(CASE WHEN status >= 400 THEN 1 ELSE 0 END) AS errors,
            AVG(response_time) AS avg_latency
        FROM logs
        WHERE timestamp >= NOW() - INTERVAL 24 HOURS
        GROUP BY hour
        ORDER BY hour DESC
    "#);
}
```

---

## Use Cases

### 1. Rapid Prototyping

**Target scenarios**: Hackathons, proof-of-concepts, client demonstrations

**Advantages**:

- Minimal boilerplate
- Instant visual feedback
- Single file applications
- Zero deployment complexity

### 2. Educational Tools

**Target scenarios**: Algorithm visualization, mathematical demonstrations, teaching materials

**Advantages**:

- LaTeX support for mathematical notation
- Interactive visualizations
- Clean, readable code for students
- Immediate execution feedback

### 3. Data Exploration

**Target scenarios**: Dataset analysis, report generation, dashboard creation

**Advantages**:

- Integrated visualization
- SQL support for complex queries (optional)
- Quick iteration cycles
- Web-based sharing

### 4. Scientific Computing

**Target scenarios**: Simulations, research notebooks, experimental visualizations

**Advantages**:

- Mathematical notation rendering
- Animation capabilities
- Numerical computation with Rust performance
- Publication-ready outputs

### 5. Business Intelligence

**Target scenarios**: Metrics dashboards, log analysis, operational monitoring

**Advantages** (with SQL feature):

- Complex aggregations
- Real-time data processing
- Interactive drill-down
- Professional visualizations

---

## Feature Selection Guidelines

### Use Default Configuration When

- Building prototypes or demos
- Working with small to medium datasets (less than 100K rows)
- Teaching programming concepts
- Creating interactive presentations
- Fast compilation is priority

### Enable SQL Feature When

- Processing large CSV/JSON files (more than 100K rows)
- Requiring complex joins and aggregations
- Building analytical dashboards
- Using window functions or Common Table Expressions
- OLAP-style queries are needed

---

## Roadmap

### Planned Features

- **Visualization**: Additional chart types (sankey, treemap, 3D plots)
- **Data Sources**: Native database connectors (PostgreSQL, MySQL)
- **Components**: Reusable widget system
- **Export**: Static HTML generation for deployment
- **Responsive Design**: Mobile-optimized interfaces

### Community Priorities

Feature prioritization is guided by:

- Ergonomic principles (readability, intuitiveness)
- Performance characteristics (safety, speed)
- Simplicity (zero-configuration approach)
- Modularity (optional features)

---

## Contributing

Contributions are welcome in the following areas:

- **Bug Reports**: [GitHub Issues]https://github.com/gerarddubard/webrust/issues
- **Feature Requests**: [GitHub Discussions]https://github.com/gerarddubard/webrust/discussions
- **Documentation**: Pull requests for documentation improvements
- **Examples**: Sharing use cases and applications

### Development Principles

1. Maintain Python-inspired ergonomics
2. Preserve Rust safety and performance guarantees
3. Keep zero-configuration philosophy
4. Ensure features remain optional when appropriate

---

## License

This project is licensed under the MIT License. See [LICENSE](LICENSE) file for details.

---

## Acknowledgments

WebRust builds upon several open-source projects:

- **DuckDB**: High-performance analytical database
- **Apache Arrow**: Columnar data format
- **tiny_http**: Lightweight HTTP server
- **serde**: Serialization framework
- **MathJax**: Mathematical notation rendering
- **ECharts**: Interactive charting library
- **Two.js**: 2D drawing library

Special thanks to the Python, Rust, and SQL communities for their contributions to programming language design and tooling.

---

## References

- Documentation: <https://docs.rs/webrust>
- Examples: <https://github.com/gerarddubard/webrust/tree/main/examples>
- Package: <https://crates.io/crates/webrust>
- Discussions: <https://github.com/gerarddubard/webrust/discussions>

---

**Version**: 1.5.0  
**Last Updated**: 2025  
**Maintainer**: See GitHub repository for current maintainer information