How do we import and use something like flask

Attempting to replicate this snippet of Flask/Python code in Mojo:
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
But I'm unsure how we replicate this in Mojo with the decorators and top level functions like this? Is it possible yet?
2 Replies
BmoVibe
BmoVibe15mo ago
I'm afraid at this moment, your options are unsurprisingly limited; but they may get you as far as you need depending on your project: A. If you're looking to import a python package without converting it to mojo, you can just call Python.import_module() with the module name:
from python import Python

# This is equivalent to Python's import numpy as np
let np = Python.import_module("numpy")

# Now use numpy as if writing in Python
array = np.array([1, 2, 3])
print(array)
from python import Python

# This is equivalent to Python's import numpy as np
let np = Python.import_module("numpy")

# Now use numpy as if writing in Python
array = np.array([1, 2, 3])
print(array)
Source: https://docs.modular.com/mojo/programming-manual.html#importing-python-modules B. If you want to convert a Python library to Mojo, a promising side project was started just 5 days ago by one of the developers that automates this process by converting Python 3.10 to Mojo. Since it's very new, you'll probably need to debug afterwards; but I would imagine the performance gains would be monumental. A link to that project can be found here: https://github.com/msaelices/py2mojo Keep in mind that you'll need to convert all of the dependencies too, so it might not even be worth it for projects like Flask. With that being said, I hope this at least answered some questions. Let me know how it goes.
GitHub
GitHub - msaelices/py2mojo: Automated Python to Mojo code translation
Automated Python to Mojo code translation. Contribute to msaelices/py2mojo development by creating an account on GitHub.
Modular Docs - Mojo🔥 programming manual
A tour of major Mojo language features with code examples.
ModularBot
ModularBot15mo ago
Congrats @Quantum Nebula, you just advanced to level 1!
Want results from more Discord servers?
Add your server