Giter VIP home page Giter VIP logo

svelte-store-anywidget's Introduction

Svelte Stores + AnyWidget

Update: use the official implementation @anywidget/svelte instead (it has a better API too!)

If you want to create svelte components on jupyter notebooks use anywidget + svelte.

To make things easier I've created an interface to create svelte writable stores that sync with anywidget. You don't even have to think about anywidget!

demo.mov

How?

Copy and paste the anywidgetStore.js file into your svelte project or just import it like I did (in Counter.svelte).

Then, in your svelte file you must register the anywidget model (returned from the render function (see main.js and Counter.svelte) in the svelte project).

<script>
	import { anywritable, register } from "./anywidgetStore"
	export let model; // anywidget model
	register(model); // register anywidget model once ever, then can use anywritable() across any component

	// these are normal svelte stores synced with your notebook
	// you change the value in the python notebook, this svelte store will automatically update
        // you change the svelte store value, the python notebook value will auotmatically update
	const count = anywritable("count"); // "count" is the name of the reactive value on the anywidget python class
</script>


<button
	on:click={() => {
		$count++;
	}}
>
	{$count}
</button>

Notice the svelte store syntax $. It's that easy!

And if you want to access the store in other components, just call const count = anywritable("count"); again in that file. It will not create another store, but give you the one you initially created.

Note that you only need to call register(model) once. Then you can call anywritable in any component.

If you want to dispose the listeners, call dispose(name) where name is the name of the traitlet/value.

Or just call disposeAll() if you're lazy like me.

Want to try it?

First install dependencies for the svelte components.

cd svelte
yarn

then run the dev server

yarn dev

Then out the example notebook called example.ipynb

Run the notebook!

Check out the svelte files to see how I import things!

Thank you internet

I borrowed half the code from Trevor Manz to get svelte up and running with anywidget.

Experimental Syntax

(WIP, still kind of buggy)

Instead of lugging around the anywritable function everywhere, this new syntax simply syncs an existing svelte store with the notebook value from anywidget.

Create the store in a file importable by any component. I'll put it in store.js for future reference.

import { writable } from "svelte/store";

export const count = writable(0);

Then when I have access to the model, I can sync the count svelte store with the notebook by calling syncAnywidget on the count. Like this

<script>
	import { onDestroy } from "svelte";

	import { count } from "./store";
	import { syncAnywidget } from "../../../anywidgetStore";

	export let model;

	// call the sync once, then the stores react to changes and the notebook reacts to store changes
	const disposeCount = syncAnywidget(count, model, "count");

	// disposes all listeners created in the sync
	onDestroy(() => {
		disposeCount();
	});
</script>

(Experimental sync syntax)

<button
	on:click={() => {
		$count++;
	}}
>
	Increment: {$count}
</button>

Now, I can simply import the store in other files, and they will be usable like normal svelte stores! No need to sync again. Just once.

Check out the CounterExperiment.svelte to see a usecase.

svelte-store-anywidget's People

Contributors

xnought avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

svelte-store-anywidget's Issues

consider just linking with existing svelte stores instead of creating a new one

Instead of calling

register(model)
const count = anywritable("count")

Create the store beforehand and simply link with the view

const count = writable(0);
const dispose = linkanywidget(count, model, "count")

This way you can more easily define the svelte store beforehand in a separate file.

downside is less readable code if you're not familiar with svelte. The current method feels like a more concise syntax.

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.