npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-svelte-transcrypt

v1.0.0

Published

Preprocessor for blocks of Python in Svelte templates (using Vite and Transcrypt)

Downloads

1

Readme

Vite Svelte Transcrypt

[proof of concept] Preprocessor for blocks of Python in Svelte templates (using Vite and Transcrypt)

pip install transcrypt
npm install --save vite-svelte-transcrypt

Usage

// example vite.config.js or vite.config.ts

import {defineConfig} from 'vite'
import {svelte} from '@sveltejs/vite-plugin-svelte'
import sveltePreprocess from 'svelte-preprocess';
import {python} from "vite-svelte-transcrypt"; // <-

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [svelte({
        preprocess: sveltePreprocess({
            aliases: [ // register lang="python" and .py files as Python
                ['py', 'python'],
                ['python', 'python'],
            ],
            python // <- 
        }),
    })],
})
<!-- SomeComponent.svelte -->
<script lang="python">`
count = 0

def increment():
    count += 1
`
</script>

<button on:click={increment}>
    count is {count}
</button>

Note: the backticks (`) are not required, but can improve Language Injection features of some editors (i.e. you can tell Pycharm the 'string' is supposed to be Python.)

Advanced

You also have access to all Javascript globals. In order to prevent the editor from complaining it doesn't know these, you can use the global keyword:


<script lang="python">`
count = 0

def interop`()
:
global
window
#
optional
global
document
#
optional
window.alert("You can now call window's methods or perform DOM manipulation with document!")
        `

</script>

Caveats

This whole project is just a proof of concept and should NOT be used in production! The whole Transcrypt JS runtime is included for every <script> with lang=python, so unless Vite does a great job of optimizing this, there is a lot of duplicate code. It is also of course much slower than just using JS. Additionally, Transcrypt does not support a lot of Python libraries at this point, so most useful imports will not work.

This module also creates a lot of junk files in the .transcrypt-build directory, since the output is used AFTER the preprocess function ends. So cleanup at the end of this function is impossible, since the files are still needed later on. If this module was used in a big project, this junk folder could get quite big after a while.

When working with JS objects from Python, it still expects the 'new' keyword. Since Python does not have this, Transcrypt provides the __new__ helper:


<script lang="python">`
def interop():
    # optional
    global Object
    global URLSearchParams
    global location
    global __new__

    print(
         Object.fromEntries(__new__(URLSearchParams(location.search)))
    )
    # = Object.fromEntries(new URLSearchParams(location.search))
`
</script>