-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from sudoskys/dev
feat():optional-dependencies, remove pydantic :(
- Loading branch information
Showing
10 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import textwrap | ||
|
||
import telegramify_markdown | ||
|
||
# Use textwrap.dedent to remove the leading whitespace from the text. | ||
md = textwrap.dedent(r""" | ||
# Title | ||
**Latex Math** | ||
Function Change: | ||
\(\Delta y = f(x_2) - f(x_1)\) can represent the change in the value of a function. | ||
Average Rate of Change: | ||
\(\frac{\Delta y}{\Delta x} = \frac{f(x_2) - f(x_1)}{x_2 - x_1}\) is used to denote the average rate of change of a function over the interval \([x_1, x_2]\). | ||
- Slope:222 | ||
\[ | ||
F = G\frac{{m_1m_2}}{{r^2}} | ||
\] | ||
\[ | ||
F = G\frac{{m_1m_2}}{{r^2}} | ||
- Inline: \(F = G\frac{{m_1m_2}}{{r^4}}\) | ||
\(A = X × \left( (P)/100 \right) × (V)/365\) | ||
\(\text{R} = \frac{\text{EXAMPLE}}{\text{Any}}\) | ||
""") | ||
|
||
# export Markdown to Telegram MarkdownV2 style. | ||
converted = telegramify_markdown.markdownify( | ||
md, | ||
max_line_length=None, # If you want to change the max line length for links, images, set it to the desired value. | ||
normalize_whitespace=False, | ||
latex_escape=False, | ||
) | ||
print(converted) | ||
# export Markdown to Telegram MarkdownV2 style. | ||
from dotenv import load_dotenv | ||
import os | ||
from telebot import TeleBot | ||
load_dotenv() | ||
telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN", None) | ||
chat_id = os.getenv("TELEGRAM_CHAT_ID", None) | ||
bot = TeleBot(telegram_bot_token) | ||
bot.send_message( | ||
chat_id, | ||
converted, | ||
parse_mode="MarkdownV2" # IMPORTANT: Need Send in MarkdownV2 Mode. | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import matplotlib.pyplot as plt | ||
from io import BytesIO | ||
|
||
|
||
def render_latex_with_matplotlib(latex_expr, bg_color="#FFFFFF", text_color="#000000", dpi=300, font_size=20): | ||
""" | ||
Renders a LaTeX math formula using matplotlib and saves it to a BytesIO object. | ||
Parameters: | ||
latex_expr (str): LaTeX math formula (e.g., r"$\int_a^b f(x) \, dx = F(b) - F(a)$") | ||
bg_color (str): Background color in HEX format (default is white: "#FFFFFF") | ||
text_color (str): Text (formula) color in HEX format (default is black: "#000000") | ||
dpi (int): Dots per inch for rendered output (default: 300). | ||
font_size (int): Font size for the formula text (default: 20). | ||
Returns: | ||
BytesIO: A BytesIO object containing the rendered formula as PNG. | ||
""" | ||
# Create a figure with transparent axes and no ticks | ||
plt.figure(figsize=(4, 2), dpi=dpi) | ||
plt.axis('off') # Hide axes | ||
|
||
# Using the 'text' function to render LaTeX | ||
plt.text(0.5, 0.5, latex_expr, color=text_color, fontsize=font_size, ha='center', va='center', | ||
transform=plt.gca().transAxes, usetex=True) # `usetex=True` enables LaTeX rendering in matplotlib | ||
|
||
# Set the figure background color | ||
plt.gcf().patch.set_facecolor(bg_color) | ||
|
||
# Save the figure directly to BytesIO | ||
buffer = BytesIO() | ||
plt.savefig(buffer, format="png", bbox_inches='tight', pad_inches=0, transparent=True) | ||
buffer.seek(0) | ||
plt.close() | ||
|
||
return buffer | ||
|
||
# Example usage: | ||
latex_formula = r"$F = G\frac{{m_1m_2}}{{r^2}}$" | ||
bg_color = "#F0F8FF" # AliceBlue background | ||
text_color = "#000000" # Black text color | ||
|
||
# Render the LaTeX math expression | ||
latex_bytesio = render_latex_with_matplotlib(latex_formula, bg_color=bg_color, text_color=text_color, font_size=30) | ||
|
||
# Save the rendered formula as a PNG file (optional) | ||
from PIL import Image | ||
|
||
img = Image.open(latex_bytesio) | ||
img.save("latex_matplotlib_rendered.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import logging | ||
|
||
# Configure the logger | ||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
logger = logging.getLogger('telegramify_markdown') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters